0

I'm having a bit of trouble trying to get my code to work

import tensorflow as tf
from tensorflow import keras
import numpy as np
import pandas as pd
import csv
from sklearn.model_selection import train_test_split

batch_size = 1

csv = "EmergeSync.csv"
val_csv = "EmergeSync.csv"

dataframe = pd.read_csv(csv)

#Split the data
train, test_ds = train_test_split(dataframe, train_size=0.8, test_size=0.2)
train_ds, val_ds = train_test_split(train, train_size=0.8, test_size=0.2)

#Building the model
model = keras.Sequential()

units = 7

model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(units=units, activation='linear'))
model.add(keras.layers.Dense(units=units, activation='linear'))
model.add(keras.layers.Dense(units=units, activation='linear'))

model.compile(optimizer="adam", loss="mean_squared_error", metrics=["accuracy"])

num_epochs = 2

history = model.fit(train_ds, epochs=num_epochs, steps_per_epoch=5, batch_size=batch_size, shuffle=True, validation_data=val_ds, verbose=1)
print(history)

I get the following error:

ValueError: No gradients provided for any variable: ['sequential/dense/kernel:0', 'sequential/dense/bias:0', 'sequential/dense_1/kernel:0', 'sequential/dense_1/bias:0', 'sequential/dense_2/kernel:0', 'sequential/dense_2/bias:0'].

I have no idea what is causing this error. If anyone could help me, that would be great!

  • Does this answer your question? [Tensorflow - No gradients provided for any variable](https://stackoverflow.com/questions/49289930/tensorflow-no-gradients-provided-for-any-variable) – Leo Gaunt Jul 31 '20 at 19:35
  • @LeoGaunt I looked at it first, but it didn't help me with my problem – Serpent Jul 31 '20 at 21:39

1 Answers1

0

So basically the error is correct there were no gradients found by the optimizer and can no longer update your network.

Now you need to ask yourself how are the gradients calculated. There are calculated by taking the partial derivative of your loss function w.r.t to all the parameters.

Your loss function is mean_square_error, so it looks something like (y-y')**2.

Here y being your original expected value and y' is what your model outputs.

If one of the two does not exist then the gradients cannot be calculated.

In your case, you are not supplying y to the model and due to this reason it is unable to calculate the gradients and unable to update the parameter values.

You will have to do the following.

history = model.fit(x=train_ds, y=np.zeros((10880,7)), epochs=num_epochs, steps_per_epoch=5, batch_size=batch_size, shuffle=True, validation_data=val_ds, verbose=1)

I do not know your y so I took dummy data, but you will have to call the fit API in an above-shown manner.

I have tried your code and it works on my system. I hope this answer finds you well.

pratsbhatt
  • 1,498
  • 10
  • 20
  • How do I find the Y in my dataset? – Serpent Aug 01 '20 at 20:53
  • For that I will have to say that it will be benificial four you if you read the basiscs of machine learning/deep learning before you write the code. As `x` and `y` should be clear to you even before starting to write the code. I do not know your dataset, but if you explain me your dataset and your goal. Probably I can try to explain you. your `x` and `y` – pratsbhatt Aug 03 '20 at 06:15