美文网首页
以numpy作为输入数据和标签数据的数据类型,训练模型

以numpy作为输入数据和标签数据的数据类型,训练模型

作者: 光光小丸子 | 来源:发表于2018-11-16 20:24 被阅读0次

使用add方法构造序贯模型,来进行模型训练

from keras.models import Sequential,Input,Model
from keras.layers import Dense,Activation
import numpy as np
model = Sequential()
model.add(Dense(32, activation='relu', input_dim=100))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])

Generate dummy data

data = np.random.random((1000, 100))
labels = np.random.randint(2, size=(1000, 1))

Train the model, iterating on the data in batches of 32 samples

model.fit(data, labels, epochs=10, batch_size=32)

使用listlayer方法构造序贯模型,来进行模型训练

model2 = Sequential([Dense(32,input_shape=(100,)),Activation('relu'),Dense(1),Activation('softmax')])

model2.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])

data = np.random.random((1000, 100))
labels = np.random.randint(2, size=(1000, 1))

Train the model, iterating on the data in batches of 32 samples

model.fit(data, labels, epochs=10, batch_size=32)

使用函数式模型,来进行模型训练

input_3 = Input(shape=(100,))
x = Dense(32,activation='relu')(input_3)
out = Dense(1,activation='softmax')(x)

model3 = Model(input_3,out)

model3.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
data = np.random.random((1000, 100))
labels = np.random.randint(2, size=(1000, 1))

Train the model, iterating on the data in batches of 32 samples

model.fit(data, labels, epochs=10, batch_size=32)

模型运行结果 dense.png

相关文章

网友评论

      本文标题:以numpy作为输入数据和标签数据的数据类型,训练模型

      本文链接:https://www.haomeiwen.com/subject/nglvfqtx.html