Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用tensorflow训练和保存多类人工神经网络模型?_Python_Tensorflow_Deep Learning_Neural Network - Fatal编程技术网

Python 如何使用tensorflow训练和保存多类人工神经网络模型?

Python 如何使用tensorflow训练和保存多类人工神经网络模型?,python,tensorflow,deep-learning,neural-network,Python,Tensorflow,Deep Learning,Neural Network,我正在尝试使用tensorflow训练一个多类分类神经网络模型。所以我有24个特征向量,以numpy数组的形式,当我打印它时看起来像这样: [[1 0 0 ... 0 1 1] [1 0 0 ... 0 1 1] [1 0 0 ... 0 1 1] ... [1 0 0 ... 2 0 0] [1 0 0 ... 2 0 0] [1 0 0 ... 2 0 0]] 上面是我想要训练的x_train数据集。它的形状是 (10799, 24) 然后,y\u列数据集如下所示 [ 3

我正在尝试使用tensorflow训练一个多类分类神经网络模型。所以我有24个特征向量,以numpy数组的形式,当我打印它时看起来像这样:

[[1 0 0 ... 0 1 1]
 [1 0 0 ... 0 1 1]
 [1 0 0 ... 0 1 1]
 ...
 [1 0 0 ... 2 0 0]
 [1 0 0 ... 2 0 0]
 [1 0 0 ... 2 0 0]]
上面是我想要训练的
x_train
数据集。它的形状是

(10799, 24)
然后,
y\u列
数据集如下所示

[   307    307    307 ... 257947 257947 257947]
它的形状是:

(10799,)
因此,
y\u train
由于标签包含不同的类别,因此显示的编号是ID。它总共有480个班级。到目前为止,我的培训尝试如下:

#Normalize the data
x_train = x_train/x_train.max()

#Convert the y_train to be one-hot encoded because they're not a regression problem, to do categorical analysis by Keras.
from keras.utils import to_categorical
y_cat_train = to_categorical(y_train)

#BUILDING THE MODEL
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(24,input_dim=24,activation='relu'))
model.add(Dense(units=48,activation='relu'))
model.add(Dense(units=96,activation='relu'))
model.add(Dense(units=192,activation='relu'))
model.add(Dense(units=384,activation='relu'))
model.add(Dense(units=420,activation='relu'))
model.add(Dense(units=450,activation='relu'))
model.add(Dense(480,activation='softmax'))

model.compile(loss='categorical_crossentropy',optimizer='rmsprop',metrics=['accuracy'])
print(model.summary())

#TRAINING THE MODEL
model.fit(x_train,y_cat_train,epochs=25)

#SAVING THE MODEL
model.save('myModel.h5')
但是我得到一个错误,说:

ValueError:形状(无,257948)和(无,480)不兼容


有人能教我或解释如何正确使用tensorflow来训练和保存多类分类的模型吗?请解释我在代码中犯了什么错误以及这个问题的可能解决方案?

您必须提供
输入\u形状
参数


#规范化数据
x_train=x_train/x_train.max()
#通过Keras进行分类分析,将y_序列转换为热编码,因为它们不是回归问题。
从keras.utils导入到_category
y_cat_train=到_分类(y_train)
#构建模型
从keras.models导入顺序
从keras.layers导入稠密
模型=tf.keras.Sequential(
[
密集层(24,activation=“relu”,input_shape=(x_train.shape[1],),
层。致密(48,活化='relu'),
层。致密(96,活化='relu'),
致密层(192,活化='relu'),
致密层(384,活化='relu'),
致密层(420,活化='relu'),
致密层(450,活化='relu'),
层密度(len(设置(y类列车)),激活=“softmax”)
]
)
compile(loss='classifical_crossentropy',optimizer='rmsprop',metrics=['accurity'])
打印(model.summary())
#训练模型
模型拟合(x_系列,y_cat_系列,历代=25)
#保存模型
model.save('myModel.h5')
注: 对于包含3个以上
密集
层的网络,您必须增加
历元
的数量才能使网络收敛

在我看来,最好只从1层开始,即具有输入功能数量的大小,然后修改网络

在您的情况下,最好从包含
len(x\u train.shape[1])
的单层开始

避免使用(如果可能)numpy数组作为X,y。相反,使用
Tensorflow
tf.数据结构强制转换数据

您可以参考以下代码示例: