“从本地文件中学习。有4760个训练数据和240个测试数据。我得到这些错误,有什么问题?”
train_dir=''
train_csv = pd.read_csv(train_dir+'train.csv')
train_images=[]
train_labels=[]
for file in train_csv['data']:
image=np.array(Image.open(train_dir+'train/'+file))
train_images.append(image)
for label in train_csv['label']:
train_labels.append(label)
train_images=np.array(train_images)
train_labels=np.array(train_labels)
test_dir=''
test_csv = pd.read_csv(test_dir+'test.csv')
test_images=[]
test_labels=[]
for file in test_csv['data']:
image=np.array(Image.open(test_dir+'test/'+file))
test_images.append(image)
for label in test_csv['label']:
test_labels.append(label)
test_images=np.array(test_images)
test_labels=np.array(test_labels)
import numpy as np
import tensorflow as tf
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD,Adam,Adagrad,RMSprop
print("train_images",train_images.shape)
print("train_labels",train_labels.shape)
print("test_images",test_images.shape)
print("test_labels",test_labels.shape)
train_images=train_images.reshape(4760,4096,3)
test_images=test_images.reshape(240,4096,3)
x_train=train_images.astype(np.float32)/255.0
x_test=test_images.astype(np.float32)/255.0
y_train=tf.keras.utils.to_categorical(train_labels,5)
y_test=tf.keras.utils.to_categorical(test_labels,5)
print("x_train",x_train.shape)
print("y_train",y_train.shape)
print("x_test",x_test.shape)
print("y_test",y_test.shape)
n_input=4096
n_hidden1=5000
n_hidden2=2500
n_hidden3=2500
n_hidden4=2500
n_output=5
batch_siz=256
n_epoch=5
def build_model():
model=Sequential()
model.add(Dense(units=n_hidden1,activation='relu',input_shape=(n_input,)))
model.add(Dense(units=n_hidden2,activation='relu'))
model.add(Dense(units=n_hidden3,activation='relu'))
model.add(Dense(units=n_hidden4,activation='relu'))
model.add(Dense(units=n_output,activation='softmax'))
return model
dmlp_adam=build_model()
dmlp_adam.compile(loss='categorical_crossentropy',optimizer=Adam(),metrics=['accuracy'])
hist_adam=dmlp_adam.fit(x_train,y_train,batch_size=batch_siz,epochs=n_epoch,validation_data=(x_test,y_test),verbose=2)
error:WARNING:tensorflow:Model was constructed with shape (None, 4096) for input KerasTensor(type_spec=TensorSpec(shape=(None, 4096), dtype=tf.float32, name='dense_input'), name='dense_input', description="created by layer 'dense_input'"), but it was called on an input with incompatible shape (None, 4096, 3).
ValueError: Exception encountered when calling layer "sequential" (type Sequential).
Input 0 of layer "dense" is incompatible with the layer: expected axis -1 of input shape to have value 4096, but received input with shape (None, 4096, 3)
Call arguments received by layer "sequential" (type Sequential):
• inputs=tf.Tensor(shape=(None, 4096, 3), dtype=float32)
• training=True
• mask=None
1条答案
按热度按时间kwvwclae1#
选择太多的神经元可能会导致过拟合,增加网络的训练时间。隐层神经元(单元)的数量最好介于输入层和输出层的大小之间。请在下面找到工作代码。