Python TensorFlow错误:ValueError:没有为任何变量提供梯度
我正在尝试运行下面的tensorflow应用程序,但我不断收到与最后一行代码相关的错误。除最后一行外,所有操作都正常运行。有人能帮忙吗Python TensorFlow错误:ValueError:没有为任何变量提供梯度,python,tensorflow,valueerror,Python,Tensorflow,Valueerror,我正在尝试运行下面的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 M
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/densite/kernel:0'、'sequential/densite/bias:0'、'sequential/densite\u 1/bias:0'、'sequential/densite\u 2/bias:0'、'sequential/densite\u 3/kernel:0'、'sequential/densite\u 4/kernel:0'、'sequential/densitive\u 4/bias:0'].我确信您的错误是因为您没有指定损失,只指定了损失权重。例如,将编译行更改为 model.compileoptimizer='adam',loss='mse'
您确定loss_weights='mse'不应该是loss='mse'吗?您必须在执行model.compile时提供loss参数。谢谢!这是错误!