简体   繁体   中英

TensorFlow Error: ValueError: No gradients provided for any variable

I'm trying to run the following tensorflow app, but I keep getting an error related to the last line of code. Everything runs properly except for the last line. Can someone please help?

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)

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', 'sequential/dense_3/kernel:0', 'sequential/dense_3/bias:0', 'sequential/dense_4/kernel:0', 'sequential/dense_4/bias:0'].

I'm fairly sure your error is because you did not specify a loss , only loss_weights . ie change you compile line to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM