【Keras】保存权重以及载入,Model、Layers函数

10 篇文章 0 订阅
1 篇文章 0 订阅
from keras.models import Sequential, Model
from keras.layers import Dense, LSTM, Activation, Input
from keras.optimizers import adam, rmsprop, adadelta
import numpy as np
import matplotlib.pyplot as plt

#construct model
data_input = Input((1,),dtype='float32',name='input_data')
x = Dense(100, activation = 'relu', name='layer1')(data_input)
x = Dense(32, activation = 'tanh', name='layer2')(x)
data_output = Dense(1, activation='tanh', name='output_data')(x)

model = Model(inputs=data_input, outputs=data_output)
model.compile(optimizer='rmsprop', loss='mse', metrics=['accuracy'])

#print model
print('models layers:',model.layers)
print('models config:',model.get_config())
print('models summary:',model.summary())

#get layers by name
layer1 = model.get_layer(name='layer1')
layer1_W_pro = layer1.get_weights()
layer2 = model.get_layer(name='layer2')
layer2_W_pro = layer2.get_weights()


#train data
dataX = np.linspace(-2 * np.pi,2 * np.pi, 1000)
dataX = np.reshape(dataX, [dataX.__len__(), 1])
noise = np.random.rand(dataX.__len__(), 1) * 0.1
dataY = np.sin(dataX) + noise

model.fit(dataX, dataY, epochs=10, batch_size=10, shuffle=True, verbose = 1)
predictY = model.predict(dataX, batch_size=1)
score = model.evaluate(dataX, dataY, batch_size=10)
print(score)
#get layers1 wights
layer1_W_end = layer1.get_weights()
#layer1_W_end - layer1_W_pro

layer2_W_end = layer2.get_weights()
#layer2_W_end - layer2_W_pro

#plot
fig, ax = plt.subplots()
ax.plot(dataX, dataY, 'b-')
ax.plot(dataX, predictY, 'r.')
ax.set(xlabel="x", ylabel="y=f(x)", title="y = sin(x),red:predict data,bule:true data")
ax.grid(True)
plt.savefig('d:\\test.eps', format='eps', dpi=1000)
plt.show()

#save weight
model.save_weights('d:\\test.hdf5')

#create new model
data_input1 = Input((1,),dtype='float32',name='input_data1')
x1 = Dense(100, activation = 'relu', name='layer11')(data_input1)
x1 = Dense(32, activation = 'tanh', name='layer21')(x1)
data_output1 = Dense(1, activation='tanh', name='output_data')(x1)

model1 = Model(inputs=data_input1, outputs=data_output1)
model1.load_weights('d:\\test.hdf5')


  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

肖恭伟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值