keras 笔记(一):自定义cell

from keras.layers import Input,Layer,LSTMCell
from keras.layers import LSTM,Bidirectional,RNN
from numpy import array
from keras.models import Model
from keras import initializers

kernel_initializer=initializers.random_uniform(seed=2)

data = array([0.1,0.1,0.1]).reshape((1,3,1))
inputs1 = Input(shape=(3,1))
inputs2 = Input(shape=(3,1))
cell=LSTMCell(2,kernel_initializer=kernel_initializer,recurrent_initializer=kernel_initializer)
layer = RNN(cell,name="RNN",trainable=True)
y = layer(inputs1)

aa=LSTM(2,name="LSTM",kernel_initializer=kernel_initializer,recurrent_initializer=kernel_initializer,trainable=True)(inputs2)

model1 = Model(inputs=inputs1,outputs=y)
model2 = Model(inputs=inputs2,outputs=aa)

model1.summary()
model2.summary()

#获得某一层的权重和偏置
weight_Dense_1 = model1.get_layer('RNN').get_weights()
weight_Dense_2 = model2.get_layer('LSTM').get_weights()
print("RNN*"*50)
print(weight_Dense_1)
print("LSTM*"*50)
print(weight_Dense_2)

print("model4--",model1.predict(data))
print("model44--",model2.predict(data))

class MinimalRNNCell(Layer):

    def __init__(self, units, **kwargs):
        self.units = units
        self.state_size = units
        super(MinimalRNNCell, self).__init__(**kwargs)

    def build(self, input_shape):
        self.kernel = self.add_weight(shape=(input_shape[-1], self.units),
                                      initializer='uniform',
                                      name='kernel')
        self.recurrent_kernel = self.add_weight(
            shape=(self.units, self.units),
            initializer='uniform',
            name='recurrent_kernel')
        self.built = True

    def call(self, inputs, states):
        prev_output = states[0]
        h = K.dot(inputs, self.kernel)
        output = h + K.dot(prev_output, self.recurrent_kernel)
        return output, [output]

data = array([0.1,0.1,0.1]).reshape((1,3,1))
inputs1 = Input(shape=(3,1))
cell=MinimalRNNCell(2)
layer = RNN(cell,name="RNN",trainable=True)
y = layer(inputs1)

model1 = Model(inputs=inputs1,outputs=y)
model1.summary()

#获得某一层的权重和偏置
weight_Dense_1 = model1.get_layer('RNN').get_weights()
print("RNN*"*50)
print(weight_Dense_1)

print("model1--",model1.predict(data))

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值