python - 在 Tensorflow 2.2.0 中,我的 model.history.history 在将数据与validation_data 一起拟合后为空
问题描述
起初它工作正常,然后我尝试在创建模型时调整一些参数,之后,
print(model.history.history)
给了我一本空字典。
如果有帮助,这是我的整个代码,
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from sklearn.metrics import mean_absolute_error
df = pd.read_csv('TF_2_Notebooks_and_Data/DATA/kc_house_data.csv')
# print(df.columns)
'''prints
Index(['id', 'date', 'price', 'bedrooms', 'bathrooms', 'sqft_living',
'sqft_lot', 'floors', 'waterfront', 'view', 'condition', 'grade',
'sqft_above', 'sqft_basement', 'yr_built', 'yr_renovated', 'zipcode',
'lat', 'long', 'sqft_living15', 'sqft_lot15'],
dtype='object')'''
# if we want to see what data column has missing data point,
# print(df.isnull()) #will print 'True' if data is missing
'''
id date price bedrooms ... lat long sqft_living15 sqft_lot15
0 False False False False ... False False False False
1 False False False False ... False False False False
2 False False False False ... False False False False
3 False False False False ... False False False False
4 False False False False ... False False False False
... ... ... ... ... ... ... ... ... ...
21592 False False False False ... False False False False
21593 False False False False ... False False False False
21594 False False False False ... False False False False
21595 False False False False ... False False False False
21596 False False False False ... False False False False
'''
# print(df.isnull().sum())
'''
id 0
date 0
price 0
bedrooms 0
bathrooms 0
sqft_living 0
sqft_lot 0
floors 0
waterfront 0
view 0
condition 0
grade 0
sqft_above 0
sqft_basement 0
yr_built 0
yr_renovated 0
zipcode 0
lat 0
long 0
sqft_living15 0
sqft_lot15 0
dtype: int64
'''
# describing the data set
# print(df.describe().transpose())
# let us see with histogram the prices of the houses
# sns.distplot(df['price'])
# counting bedrooms per house
# sns.countplot(df['bedrooms'])
# removing unwanted data
df = df.drop('id', axis=1)
# changing data style to yyyy-mm-dd
df['date'] = pd.to_datetime(df['date'])
# extracting year from date
df['year'] = df['date'].apply(lambda date: date.year)
df['month'] = df['date'].apply(lambda date: date.month)
# checking if prices are affected by year
# sns.scatterplot(x=df['price'],y=df['month'],hue=df['year'])
# or
# sns.boxplot('month','price',data=df)
# or
# print(df.groupby('month').mean()['price'].plot())
# removing date column
df = df.drop('date', axis=1)
# also drop zipcodes
df = df.drop('zipcode', axis=1)
# print(df['yr_renovated'].value_counts())
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.3, random_state=101)
scaler = MinMaxScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# print(X_train.shape)
# prints (15117, 19)
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, activation=None))
model.compile(optimizer='adam', loss='mse')
# adding validation data will not affect the weights and the biases of the model, it is to get an idea of,
# over-fitting or under-fitting the data
#reducing the batch size will make the model more time to train but less over-fitting will occur
model.fit(X_train, y_train, validation_data=(X_test, y_test),
batch_size=128, epochs=4,verbose=2)
predictions = model.predict(X_test)
# checking if we are over-fitting or no
print(f"model hist is : \n {model.history.history}")
losses = pd.DataFrame(model.history.history)
print(losses)
#losses.plot()
# NOTE: the line curve for loss must match for not over-fitting the data.
#plt.ylabel('losses')
#plt.xlabel('number of epochs')
off_by = mean_absolute_error(y_test, predictions)
print(f"the predictions are off by {off_by} dollars")
print(f"the mean of all the prices is {df['price'].mean()}")
plt.show()
输出:
Epoch 1/4
119/119 - 0s - loss: 430244003840.0000 - val_loss: 418937962496.0000
Epoch 2/4
119/119 - 0s - loss: 429396754432.0000 - val_loss: 415953223680.0000
Epoch 3/4
119/119 - 0s - loss: 417119928320.0000 - val_loss: 387559292928.0000
Epoch 4/4
119/119 - 0s - loss: 354640822272.0000 - val_loss: 283466629120.0000
model hist is :
{}
Empty DataFrame
Columns: []
Index: []
the predictions are off by 401518.14752604166 dollars
the mean of all the prices is 540296.5735055795
Process finished with exit code 0
我不知道现在该去哪里,这条线:
print(f"model hist is : \n {model.history.history}")
印刷:
model hist is :
{}
因为我需要分析损失和验证损失,所以我无法进一步了解
解决方案
history = model.fit(...)
print(f"model hist is : \n {history.history}")
推荐阅读
- java - 如何修复netbeans中的getrowcount()?
- android-studio - RecylerView to pdf
- android - Xamarin.Android 谷歌地图导航模糊
- c# - 硒的 1 行中有 2 个发送键
- python - Python 逻辑不会增加太多时间复杂度
- django - ValueError:字典更新序列元素 #0 的长度为 25;使用字典解包元组时需要 2
- c# - 如何在 dot net 标准应用程序中使用 Dapper 从数据库中读取块中的文件
- iphone - 在 SwiftUI 中,如何使用核心数据将触摸 ID 存储为人员/类的属性并记录它们?
- r - R - 自动排除 quantile_split 函数
- c# - 如何将文件转换为base 64