现在开始学习keras,作为一个小白,先写一个一个简单的序贯模型和函数式模型
import numpyas np
from keras.layersimport Embedding
from keras.modelsimport Sequential,Input,Model
##序贯式编程
model = Sequential()
model.add(Embedding(1000,64,input_length=10))
input_array = np.random.randint(1000,size=(32,10))
model.compile('rmsprop','mse')
output_array = model.predict(input_array)
assert output_array.shape == (32,10,64)
print('first')
print(output_array.shape)
###函数式模型
a_input = Input(shape=(10,))
ember = Embedding(1000,64,input_length=10)(a_input)
model = Model(inputs = a_input,outputs=ember)
input_array = np.random.randint(1000,size=(32,10))
model.compile('rmsprop',loss ='categorical_crossentropy',metrics=['mse'])
output_array = model.predict(input_array)
assert output_array.shape == (32,10,64)
print('second')
print(output_array.shape)
网友评论