【发布时间】:2021-03-14 20:28:26
【问题描述】:
这是我的代码:
# Library Imports
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
plt.style.use("ggplot")
from keras.models import Sequential
from keras.layers import LSTM, Dense, Dropout
# Loading/Reading in the Data
df = pd.read_csv("BTC-USD.csv")
# Data Preprocessing
### Setting the datetime index as the date, only selecting the 'Close' column, then only the last 1000 closing prices.
df = df.set_index("Date")[['Close']].tail(3000)
df = df.set_index(pd.to_datetime(df.index))
# Normalizing/Scaling the Data
scaler = MinMaxScaler()
df = pd.DataFrame(scaler.fit_transform(df), columns=df.columns, index=df.index)
def visualize_training_results(results):
"""
Plots the loss and accuracy for the training and testing data
"""
history = results.history
plt.figure(figsize=(12,4))
plt.plot(history['val_loss'])
plt.plot(history['loss'])
plt.legend(['val_loss', 'loss'])
plt.title('Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.show()
plt.figure(figsize=(12,4))
plt.plot(history['val_accuracy'])
plt.plot(history['accuracy'])
plt.legend(['val_accuracy', 'accuracy'])
plt.title('Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.show()
def split_sequence(seq, n_steps_in, n_steps_out):
"""
Splits the univariate time sequence
"""
X, y = [], []
for i in range(len(seq)):
end = i + n_steps_in
out_end = end + n_steps_out
if out_end > len(seq):
break
seq_x, seq_y = seq[i:end], seq[end:out_end]
X.append(seq_x)
y.append(seq_y)
return np.array(X), np.array(y)
def layer_maker(n_layers, n_nodes, activation, drop=None, d_rate=.5):
"""
Create a specified number of hidden layers for an RNN
Optional: Adds regularization option, dropout layer to prevent potential overfitting if necessary
"""
# Creating the specified number of hidden layers with the specified number of nodes
for x in range(1,n_layers+1):
model.add(LSTM(n_nodes, activation=activation, return_sequences=True))
# Adds a Dropout layer after every Nth hidden layer (the 'drop' variable)
try:
if x % drop == 0:
model.add(Dropout(d_rate))
except:
pass
# How many periods looking back to train
n_per_in = 30
# How many periods ahead to predict
n_per_out = 10
# Features (in this case it's 1 because there is only one feature: price)
n_features = 1
# Splitting the data into appropriate sequences
X, y = split_sequence(list(df.Close), n_per_in, n_per_out)
# Reshaping the X variable from 2D to 3D
X = X.reshape((X.shape[0], X.shape[1], n_features))
# Instantiating the model
model = Sequential()
# Activation
activ = "softsign"
# Input layer
model.add(LSTM(30, activation=activ, return_sequences=True, input_shape=(n_per_in, n_features)))
# Hidden layers
layer_maker(n_layers=6, n_nodes=12, activation=activ)
# Final Hidden layer
model.add(LSTM(10, activation=activ))
# Output layer
model.add(Dense(n_per_out))
# Model summary
model.summary()
plt.figure(figsize=(12,5))
#Im compiling the code here before training
model.compile()
#visualize_training_results(res)
res = model.fit(X, y, epochs=800, batch_size=32, validation_split=0.1)
#visualize_training_results(res)
我需要在这里做什么才能开始训练我的模型?
我也试过这段代码编译:
model.compile(
optimizer='adam',
loss='mean_squared_error',
metrics=[
metrics.MeanSquaredError(),
metrics.AUC(),
]
)
但是当我运行这个时,我得到未定义的指标错误。 即使我在编译函数中定义了它?
我也这样做了
model.compile(loss='categorical_crossentropy', optimizer='adam')
开始训练,但我得到了这个输出
Epoch 6/800
10/10 [==============================] - 1s 53ms/step - loss: nan - val_loss: nan
Epoch 7/800
10/10 [==============================] - 0s 43ms/step - loss: nan - val_loss: nan
当我这样做时:
model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
我得到这个输出
0/10 [==============================] - 0s 49ms/step - loss: nan - accuracy: 0.1128 - val_loss: nan - val_accuracy: 0.0606
Epoch 121/800
10/10 [==============================] - 0s 48ms/step - loss: nan - accuracy: 0.1086 - val_loss: nan - val_accuracy: 0.0606
它训练并且准确度有效,但我仍然得到 nan 的 loss 和 val_loss
不管我用什么编译函数参数,这个东西都不计算损失
现在我试过了
model.compile(
optimizer='adam',
loss='mean_squared_error',
metrics=["accuracy"],
)
这就是我得到的
10/10 [==============================] - 1s 74ms/step - loss: nan - accuracy: 0.0953 - val_loss: nan - val_accuracy: 0.0606
Epoch 5/800
10/10 [==============================] - 1s 74ms/step - loss: nan - accuracy: 0.1097 - val_loss: nan - val_accuracy: 0.0606
Epoch 6/800
10/10 [==============================] - 1s 72ms/step - loss: nan - accuracy: 0.0929 - val_loss: nan - val_accuracy: 0.0606
【问题讨论】:
-
尝试只在顶部导入 keras,例如
import keras -
TF 和 Keras 用户文档中涵盖了大部分(如果不是全部)您正在做的事情。 “出了点问题”表明您尚未仔细阅读这些用户指南——您应该有一个请提供预期的MRE - Minimal, Reproducible Example,并清楚地说明您哪里出了问题。
标签: python tensorflow keras data-science