TensorFlow Error: ValueError: No gradients provided for any variable
TensorFlow Error: ValueError: No gradients provided for any variable
我正在尝试 运行 以下 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
,只指定了 loss_weights
。即,将您的编译行更改为
model.compile(optimizer='adam', loss='mse')
我正在尝试 运行 以下 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
,只指定了 loss_weights
。即,将您的编译行更改为
model.compile(optimizer='adam', loss='mse')