【问题标题】:TensorFlow Error: ValueError: No gradients provided for any variableTensorFlow 错误:ValueError:没有为任何变量提供梯度
【发布时间】:2020-08-28 08:42:06
【问题描述】:

我正在尝试运行以下 tensorflow 应用程序,但我不断收到与最后一行代码相关的错误。除了最后一行之外,一切都运行正常。有人可以帮忙吗?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_absolute_error, mean_squared_error

from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense
from tensorflow.python.keras.models import load_model

df = pd.read_csv('kc_house_data.csv')
print(f"df.head():\n{df.head()}")

print(f"df.isnull().sum():\n{df.isnull().sum()}")

print(f"df.describe().transpose():\n{df.describe().transpose()}")

corr = df.corr()
print(f"corr:\n{corr}")

corr_sorted = corr['price'].sort_values()      
sort_df = df.sort_values('price', ascending=False)         
non_top_1_perc = sort_df.iloc[216:]

print(f"df.head(): {df.head()}")

df = df.drop('id', axis=1)

#convert do datetime
df['date'] = pd.to_datetime(df['date'])
#feature engineering
#extracting the year & month
df['year'] = df['date'].apply(lambda date: date.year)
df['month'] = df['date'].apply(lambda date: date.month)

monthly_prices = df.groupby('month').mean()['price']
#monthly_prices.plot()
#plt.show()
print(f"monthly_prices: {monthly_prices}")

yearly_prices = df.groupby('year').mean()['price']
print(f"yearly_prices: {yearly_prices}")

df = df.drop('date', axis=1)

df = df.drop('zipcode', axis=1)

#sklearn
X = df.drop('price', axis=1).values
y = df['price'].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=101)

#perform the scaling to prevent data leakage from the test set
scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train)
#do not fit to your test set because you don't want to assume prior information
X_test = scaler.transform(X_test)

X_train.shape

#tensorflow
model = Sequential()
model.add(Dense(19, activation='relu'))
model.add(Dense(19, activation='relu'))
model.add(Dense(19, activation='relu'))
model.add(Dense(19, activation='relu'))
model.add(Dense(1))

model.compile(optimizer='adam', loss_weights='mse')

model.fit(x=X_train, y=y_train, validation_data=(X_test, y_test), batch_size=128, epochs=400)

错误:

ValueError: 没有为任何变量提供梯度:['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'、'sequential/dense_3/kernel:0'、'sequential/dense_3/bias:0'、'sequential/dense_4/kernel: 0', 'sequential/dense_4/bias:0'].

【问题讨论】:

  • 你确定loss_weights='mse'不应该是loss='mse'吗?
  • 在执行model.compile时必须提供loss参数。
  • 谢谢!这是错误!

标签: python tensorflow valueerror


【解决方案1】:

我很确定您的错误是因为您没有指定loss,而只指定了loss_weights。即将编译行更改为

model.compile(optimizer='adam', loss='mse')

【讨论】:

    猜你喜欢
    • 2021-07-11
    • 2020-10-31
    • 2020-09-30
    • 2021-09-08
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    相关资源
    最近更新 更多