【发布时间】:2020-01-02 21:14:19
【问题描述】:
尝试在 Keras 和 Tensorflow 后端训练我的模型时遇到问题。
import numpy as np
from sklearn.utils import shuffle
# Load Data
df = np.loadtxt("features.txt", delimiter=',')
print('Features shape:', df.shape)
labels = np.loadtxt("labels.txt", delimiter=',')
print('Labels shape', labels.shape)
# Replace 10 by 0
labels = np.where(labels == 10, 0, labels)
# Randomize the data
data_shuffled, labels_shuffled = shuffle(df, labels, random_state=42)
from keras.models import Sequential
from keras.layers import Dense
# split into input (X) and output (y) variables
X = data_shuffled[:4000]
y = data_shuffled[4000:]
print(X.shape, y.shape)
# define the keras model
model = Sequential()
model.add(Dense(12, input_dim=400, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(10, activation='sigmoid'))
# compile the keras model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit the keras model on the dataset
model.fit(X, y, epochs=1000, batch_size=100, verbose=0)
# make class predictions with the model
predictions = model.predict_classes(X)
# summarize the first 5 cases
for i in range(5):
print('%s => %d (expected %d)' % (X[i].tolist(), predictions[i], y[i]))
# evaluate the keras model
accuracy = model.evaluate(X, y)
print('Accuracy: %.2f' % (accuracy*100))
特征形状:(5000, 400) 标签形状 (5000,)
我在调用 model.fit() 的那一行遇到了错误。
我尝试修复此错误的方法: 我尝试使用以下代码重塑 X 和 y numpy 数组: X.reshape(400, -1) y.reshape(400, -1)
但这无济于事。
【问题讨论】: