简体   繁体   English

我收到一个 ValueError:没有为任何变量提供渐变

[英]I'm getting a ValueError: No gradients provided for any variable

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!如果有人可以帮助我,那就太好了!

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.通过对所有参数取损失 function w.r.t 的偏导数来计算。

Your loss function is mean_square_error , so it looks something like (y-y')**2 .你的损失 function 是mean_square_error ,所以它看起来像(y-y')**2

Here y being your original expected value and y' is what your model outputs.这里y是您的原始期望值, y'是您的 model 输出的值。

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.在您的情况下,您没有向 model 提供y ,因此它无法计算梯度并且无法更新参数值。

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.我不知道你的y所以我拿了虚拟数据,但你必须以上面显示的方式调用fit API。

I have tried your code and it works on my system.我已经尝试过您的代码,它可以在我的系统上运行。 I hope this answer finds you well.我希望这个答案能找到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在 keras 中使用 train_step() 时会收到错误“ValueError:没有为任何变量提供梯度:”? - Why am I getting the error "ValueError: No gradients provided for any variable:" while using train_step() in keras? 在培训期间,我收到以下错误。 “ ValueError:没有为任何变量提供梯度” - Whie training I am getting the following error. “ ValueError: No gradients provided for any variable” ValueError:没有为任何变量提供渐变:['Variable:0'] - ValueError: No gradients provided for any variable: ['Variable:0'] TensorFlow 错误:ValueError:没有为任何变量提供梯度 - TensorFlow Error: ValueError: No gradients provided for any variable TF Keras - ValueError:没有为任何变量提供梯度 - TF Keras - ValueError: No gradients provided for any variable Keras ValueError:没有为任何变量提供梯度 - Keras ValueError: No gradients provided for any variable ValueError:没有为策略梯度中的任何变量提供梯度 - ValueError: No gradients provided for any variable in policy gradient Tensorflow ValueError:没有为任何变量提供梯度: - Tensorflow ValueError: No gradients provided for any variable: 训练 CNN:ValueError:没有为任何变量提供梯度 - Training CNN: ValueError: No gradients provided for any variable Adam 优化器:ValueError:没有为任何变量提供梯度 - Adam optimizer: ValueError: No gradients provided for any variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM