def __init__(self, units): super(LSTMUsingLSTMCell, self).__init__() # [b, 64] self.state0 = [ tf.zeros([batch_size, units]), tf.zeros([batch_size, units]) ] self.state1 = [ tf.zeros([batch_size, units]), tf.zeros([batch_size, units]) ] # transform text to embedding representation # [b, 80] => [b, 80, 100] self.embedding = layers.Embedding(total_words, embedding_len, input_length=max_review_len) # [b, 80, 100] , h_dim: 64 # RNN: cell1 ,cell2, cell3 # SimpleRNN self.rnn_cell0 = layers.LSTMCell(units, dropout=0.5) self.rnn_cell1 = layers.LSTMCell(units, dropout=0.5) # fc, [b, 80, 100] => [b, 64] => [b, 1] self.output_layer = layers.Dense(1)
def __init__(self, lstm_hidden_num): super(LstmDecoder, self).__init__() self.lstm_hidden_num = lstm_hidden_num k_initializer = tf.keras.initializers.truncated_normal() b_initializer = tf.keras.initializers.zeros() forward_layer = layers.LSTMCell(self.lstm_hidden_num, dropout=0.8, recurrent_dropout=0.8, kernel_initializer=k_initializer, bias_initializer=b_initializer) backward_layer = layers.LSTMCell( self.lstm_hidden_num, dropout=0.8, recurrent_dropout=0.8, kernel_initializer=k_initializer, bias_initializer=b_initializer, ) forward_layer = layers.RNN(forward_layer, return_sequences=True) backward_layer = layers.RNN(backward_layer, return_sequences=True, go_backwards=True) self.bilstm = layers.Bidirectional(forward_layer, backward_layer=backward_layer) self.W = tf.Variable(initial_value=lambda: tf.random.truncated_normal( shape=[self.lstm_hidden_num * 2, config.NUM_CLASSES], stddev=0.1), trainable=True) self.b = tf.Variable( initial_value=lambda: tf.constant(0., shape=[config.NUM_CLASSES]), trainable=True)
def LSTMCell4D(inp, mdlstm_units, dense_units, return_sequences=False, dense_act='tanh'): w = layers.RNN(layers.LSTMCell(mdlstm_units), return_sequences=True)(inp) w = layers.Dense(dense_units, activation=dense_act)(w) x = lambda_reverse_layer_A()(inp) x = layers.RNN(layers.LSTMCell(mdlstm_units), return_sequences=True)(x) x = layers.Dense(dense_units, activation=dense_act)(x) x = lambda_reverse_layer_A()(x) y = lambda_reverse_layer_B()(inp) y = layers.RNN(layers.LSTMCell(mdlstm_units), return_sequences=True)(y) y = layers.Dense(dense_units, activation=dense_act)(y) y = lambda_reverse_layer_B()(y) z = lambda_reverse_layer_A()(inp) z = lambda_reverse_layer_B()(z) z = layers.RNN(layers.LSTMCell(mdlstm_units), return_sequences=True)(z) z = layers.Dense(dense_units, activation=dense_act)(z) z = lambda_reverse_layer_B()(z) z = lambda_reverse_layer_A()(z) added = layers.Add()([w, x, y, z]) return added
def inference(x, n_in=None, maxlen=None, n_hidden=None, n_out=None): def weight_bariable(shape, name=None): initial = tf.truncated_normal(shape, stddev=0.01) return tf.Variable(initial, name=name) def bias_variable(shape, name=None): initial = tf.zeros(shape, dtype=tf.float32) return tf.Variable(initial, name=name) # In order to adjust to specification of tf.nn.static_bidirectional_rnn, # reshaping format of recurrent data to (batch_size, input_dim) x = tf.transpose( x, [1, 0, 2]) # Tensor: (?, MAXLEN, N_IN) => Tensor: (MAXLEN, ?, N_IN) x = tf.reshape( x, [-1, n_in]) # Tensor: (MAXLEN, ?, N_IN) => Tensor: (?, N_IN) x = tf.split(x, maxlen, 0) # Tensor: (?, N_IN) => list: len(x): MAXLEN cell_forward = layers.LSTMCell(n_hidden, unit_forget_bias=True) cell_backward = layers.LSTMCell(n_hidden, unit_forget_bias=True) outputs, _, _ = tf.nn.static_bidirectional_rnn(cell_forward, cell_backward, x, dtype=tf.float32) W = weight_bariable([n_hidden * 2, n_out], name='W') tf.summary.histogram('W', W) # TensorBoard b = bias_variable([n_out], name='b') y = tf.matmul(outputs[-1], W) + b return y
def build_LSTMCellwRNN_model(mdlstm_units=32, dense_units=200): dense_act = 'tanh' input_img = layers.Input(shape=(max_img_width, max_img_height, 1), name='image', dtype='float32') labels = layers.Input(name='label', shape=(None, ), dtype='float32') input_reshaped = layers.Reshape(target_shape=(max_img_width, max_img_height))(input_img) x = layers.RNN(layers.LSTMCell(mdlstm_units), return_sequences=True)(input_reshaped) x = layers.Dense(100, activation=dense_act, name='x_out')(x) y = layers.Permute((2, 1))(input_reshaped) y = layers.RNN(layers.LSTMCell(mdlstm_units), return_sequences=True)(y) y = layers.Dense(200, activation=dense_act, name='y_out')(y) y = layers.Permute((2, 1))(y) print(x) print(y) added = layers.Add()([x, y]) out = layers.Dense(len(alphabet) + 1, activation='softmax', name='dense_out')(added) classified = CTCLayer(name='ctc_loss')(labels, out) model = keras.models.Model(inputs=[input_img, labels], outputs=classified, name='LSTMlayerModel') model.compile(optimizer=keras.optimizers.Adam()) return model
def __init__(self, units): super(LSTM, self).__init__() # [b, 64] self.state0 = [ tf.zeros([batch_size, units]), tf.zeros([batch_size, units]) ] self.state1 = [ tf.zeros([batch_size, units]), tf.zeros([batch_size, units]) ] # transform text to embedding representation # [b, 100] => [b, 100, 150] self.embedding = layers.Embedding(input_dim=total_words, output_dim=embedding_len, input_length=max_review_len) # units=64 self.rnn_cell0 = layers.LSTMCell(units, dropout=0.5) self.rnn_cell1 = layers.LSTMCell(units, dropout=0.5) # 全连接层 # [b, 100, 150] => [b, 64] => [b, 1] self.out = layers.Dense(1)
def __init__(self, units): super(Myrnn, self).__init__() self.state0 = [tf.zeros([batchsz, units]), tf.zeros([batchsz, units])] self.state1 = [tf.zeros([batchsz, units]), tf.zeros([batchsz, units])] self.embedding = layers.Embedding(total_words, embedding_len, input_length=max_len) self.rnn_cell0 = layers.LSTMCell(units, dropout=0.2) self.rnn_cell1 = layers.LSTMCell(units, dropout=0.2) self.fc = layers.Dense(1)
def __init__(self, units): super(MyLSTM, self).__init__() self.state0 = [ tf.zeros(shape=(batch_size, units)), tf.zeros(shape=(batch_size, units)) ] self.state1 = [ tf.zeros(shape=(batch_size, units)), tf.zeros(shape=(batch_size, units)) ] self.embedding = layers.Embedding(num_words, 100, input_length=seq_len) self.cell0 = layers.LSTMCell(units, dropout=0.5) self.cell1 = layers.LSTMCell(units, dropout=0.5) self.outlayer = layers.Dense(1)
def get_encoder(hidden_size,vocab_size, num_tokens=7,nlayers=1,dropout=0.2, bsize=57, msize=552, ssize=3190, dsize=404): len_input=keras.Input(shape=(),name='len',dtype=tf.int64) pieces_input=[keras.Input(shape=(num_pieces,),name='piece{}'.format(i+1)) for i in range(num_tokens)] embedding=layers.Embedding(vocab_size,200,mask_zero=True) pieces=[embedding(piece) for piece in pieces_input] cells=[layers.LSTMCell(hidden_size,dropout=dropout) for _ in range(nlayers-1)] cells.append(layers.LSTMCell(hidden_size)) lstm=layers.RNN(cells,return_sequences=False,return_state=True,name='multi-lstm') state=lstm(pieces[0])[-1][-1] states=[state] pieces.remove(pieces[0]) zero_state=tf.zeros_like(state) sent_len=tf.reshape(len_input,(-1,1)) for i,piece in enumerate(pieces): # for piece in pieces: state=tf.where(i+1<sent_len,lstm(piece)[-1][-1],zero_state) # state= lstm(piece)[-1][-1] states.append(state) result=tf.math.add_n(states) #sent_len=tf.tile(len_input,[1,hidden_size]) sent_len=tf.cast(sent_len,tf.float32) result=tf.divide(result,sent_len) feature=Sequential([ layers.Dense(hidden_size,activations.relu), layers.Dropout(dropout), layers.Dense(hidden_size,activations.relu,name='final_feature') ],name='feature_seq')(result) bcate = layers.Dense(bsize,name='bcateid')(feature) mcate = layers.Dense(msize,name='mcateid')(feature) scate = layers.Dense(ssize,name='scateid')(feature) dcate = layers.Dense(dsize,name='dcateid')(feature) inputs=[len_input]+pieces_input model=Model(inputs=inputs,outputs=[bcate,mcate,scate,dcate]) return model
def __init__(self, f_gcn, f_atten, n_units, le_hs, ld_hs, state_dim): super(ScaleNet, self).__init__() self.f_gcn = f_gcn # [5,16,32,64] self.f_atten = f_atten # [16,16,16] self.n_units = n_units # [256,128,64,32,8,2] self.le_hs = le_hs # lstm_encoder hidden_size self.ld_hs = ld_hs # lstm_decoder hidden_size self.state_dim = state_dim # state dim self.thresold = 1e-6 # eps self.egcn_block = EGCNBLOCK(f_gcn, f_atten) self.lstm_encoder = layers.LSTMCell(self.le_hs) self.lstm_decoder = layers.LSTMCell(self.ld_hs) self.mlps = MLPBLOCK(self.n_units)
def __init__( self, rnn_units: int, max_length: int, vocab_size: int, embedding_units: int, attention_units: int, num_decoder_cells: int = 2, dropout_prob: float = 0.0, ) -> None: super().__init__() self.vocab_size = vocab_size self.max_length = max_length self.embed = layers.Dense(embedding_units, use_bias=False) self.embed_tgt = layers.Embedding(embedding_units, self.vocab_size + 1) self.lstm_cells = layers.StackedRNNCells([ layers.LSTMCell(rnn_units, implementation=1) for _ in range(num_decoder_cells) ]) self.attention_module = AttentionModule(attention_units) self.output_dense = layers.Dense(self.vocab_size + 1, use_bias=True) self.dropout = layers.Dropout(dropout_prob)
def __init__( self, search_space, embedding_size, hidden_size, dropout, ): super(Decoder, self).__init__() self.embedding_size = embedding_size self.hidden_size = hidden_size self.vocal_size = self.ss.vocal_size + 1 # 0-th is for the initial input self.dropout = dropout self.ss = search_space self.input_embedding = layers.Embedding( self.vocal_size, self.embedding_size) # for auto-aggressive self.dropout = layers.Dropout(self.dropout) self.lstm_cell = layers.LSTMCell(self.hidden_size, dropout=self.dropout) self.output_classifier = [] for i in range(self.ss.count * self.ss.num_layers): # different kinds of tokens self.decoders.append(keras.layers.Dense(units=self.ss[i]['size']))
def __init__(self, access_config, controller_config, output_size, clip_value=None, name='dnc'): """Initializes the DNC core. Args: access_config: dictionary of access module configurations. controller_config: dictionary of controller (LSTM) module configurations. output_size: output dimension size of core. clip_value: clips controller and core output values to between `[-clip_value, clip_value]` if specified. name: module name (default 'dnc'). Raises: TypeError: if direct_input_size is not None for any access module other than KeyValueMemory. """ super().__init__(name=name) self._controller = layers.LSTMCell(**controller_config) self._access = access.MemoryAccess(**access_config) self._output_size = output_size self._clip_value = clip_value or 0 self.out_layer = layers.Dense(self._output_size, name='output_linear')
def __init__(self, action_space): super(RecurrentDuelingQNetwork, self).__init__() self.action_space = action_space self.conv1 = kl.Conv2D(32, 8, strides=4, activation="relu", kernel_initializer="he_normal") self.conv2 = kl.Conv2D(64, 4, strides=2, activation="relu", kernel_initializer="he_normal") self.conv3 = kl.Conv2D(64, 3, strides=1, activation="relu", kernel_initializer="he_normal") self.flatten1 = kl.Flatten() self.lstm = kl.LSTMCell(512) self.value = kl.Dense(1, kernel_initializer="he_normal") self.advantages = kl.Dense(self.action_space, kernel_initializer="he_normal")
def __init__(self, units): super(My_lstm, self).__init__() self.state0 = [ tf.zeros([Params.BATCH_SIZE, units]), tf.zeros([Params.BATCH_SIZE, units]) ] self.state1 = [ tf.zeros([Params.BATCH_SIZE, units]), tf.zeros([Params.BATCH_SIZE, units]) ] self.embedding = layers.Embedding(Params.NUM_WORD, Params.EMBEDDING_DIM, input_length=Params.MAX_LEN) self.lstmcell0 = layers.LSTMCell(units=units, dropout=Params.DROP_OUT) self.lstmcell1 = layers.LSTMCell(units=units, dropout=Params.DROP_OUT) self.outlayers = layers.Dense(1)
def _stacked_lstm_impl2(self, dim): rnn_cells = [layers.LSTMCell(dim) for _ in range(self.num_layers)] stacked_lstm = layers.StackedRNNCells(rnn_cells) lstm_layer = layers.RNN(stacked_lstm, return_sequences=True) if self.bidirectional: lstm_layer = layers.Bidirectional(lstm_layer) return [lstm_layer]
def __init__(self, config): self.stack_rnn_size = config.stack_rnn_size # xy encoder: [N,T1,h_dim] super(TrajectoryEncoder, self).__init__(name="trajectory_encoder") # Linear embedding of the observed positions (for each x,y) self.traj_xy_emb_enc = layers.Dense(config.emb_size, activation=config.activation_func, use_bias=True, name='position_embedding') # LSTM cell, including dropout, with a stacked configuration. # Output is composed of: # - the sequence of h's along time, from the highest level only: h1,h2,... # - last pair of states (h,c) for the first layer # - last pair of states (h,c) for the second layer # - ... and so on self.lstm_cells= [layers.LSTMCell(config.enc_hidden_size, name = 'trajectory_encoder_cell', dropout= config.dropout_rate, recurrent_dropout=config.dropout_rate) for _ in range(self.stack_rnn_size)] self.lstm_cell = layers.StackedRNNCells(self.lstm_cells) # Recurrent neural network using the previous cell # Initial state is zero; We return the full sequence of h's and the pair of last states self.lstm = layers.RNN(self.lstm_cell, name = 'trajectory_encoder_rnn', return_sequences= True, return_state = True)
def get_cell(cell_type, units): cell_type = cell_type.lower() if cell_type == 'lstm': return layers.LSTMCell(units=units) if cell_type == 'gru': return layers.GRUCell(units=units) if cell_type == 'simple': return RNNCell(units=units) raise ValueError('Unknown RNN cell type')
def __init__(self, config): super(DecoderAtt, self).__init__(name="trajectory_decoder") self.add_social = config.add_social self.stack_rnn_size = config.stack_rnn_size self.rnn_type = config.rnn_type # Linear embedding of the encoding resulting observed trajectories self.traj_xy_emb_dec = layers.Dense(config.emb_size, activation=config.activation_func, name='trajectory_position_embedding') # RNN cell # Condition for cell type if self.rnn_type == 'gru': # GRU cell self.dec_cell_traj = layers.GRUCell(config.dec_hidden_size, recurrent_initializer='glorot_uniform', dropout=config.dropout_rate, recurrent_dropout=config.dropout_rate, name='trajectory_decoder_GRU_cell') else: # LSTM cell self.dec_cell_traj = layers.LSTMCell(config.dec_hidden_size, recurrent_initializer='glorot_uniform', name='trajectory_decoder_LSTM_cell', dropout=config.dropout_rate, recurrent_dropout=config.dropout_rate) # RNN layer self.recurrentLayer = layers.RNN(self.dec_cell_traj,return_sequences=True,return_state=True) self.M = 1 if (self.add_social): self.M=self.M+1 # Attention layer self.focal_attention = FocalAttention(config,self.M) # Dropout layer self.dropout = layers.Dropout(config.dropout_rate,name="dropout_decoder_h") # Mapping from h to positions self.h_to_xy = layers.Dense(config.P, activation=tf.identity, name='h_to_xy') # Input layers # Position input dec_input_shape = (1,config.P) self.input_layer_pos = layers.Input(dec_input_shape,name="position") enc_last_state_shape = (config.dec_hidden_size) # Proposals for inital states self.input_layer_hid1= layers.Input(enc_last_state_shape,name="initial_state_h") self.input_layer_hid2= layers.Input(enc_last_state_shape,name="initial_state_c") # Context shape: [N,M,T1,h_dim] ctxt_shape = (self.M,config.obs_len,config.enc_hidden_size) # Context input self.input_layer_ctxt = layers.Input(ctxt_shape,name="context") self.out = self.call((self.input_layer_pos,(self.input_layer_hid1,self.input_layer_hid2),self.input_layer_ctxt)) # Call init again. This is a workaround for being able to use summary super(DecoderAtt, self).__init__( inputs= [self.input_layer_pos,self.input_layer_hid1,self.input_layer_hid2,self.input_layer_ctxt], outputs=self.out)
def __init__(self, units): super(MyLSTM, self).__init__() # 下面写具体的网络层 # Embedding层用于model的第一层 # [b, 80] => [b, 80, 100] self.embedding = layers.Embedding(total_words, hidden_num, input_length=max_review_len) # LSTMCell层,设置两个LSTM层 # [b, 80, 100] => [b, 64] Sequence => Vector self.LSTM0 = layers.LSTMCell(units, dropout=0.2) self.LSTM1 = layers.LSTMCell(units, dropout=0.2) # Dense层,为了表示0~1转换为一个数 # [b, 64] => [b, 1] self.outlayer = layers.Dense(1) self.state0 = [tf.zeros([batchsz, units]), tf.zeros([batchsz, units])] self.state1 = [tf.zeros([batchsz, units]), tf.zeros([batchsz, units])]
def __init__(self, audio_features=195, audio_window_size=8, stage2_window_size=64, num_face_ids=76, num_landmarks=76, num_phonemes=21, num_visemes=20, dropout_rate=0.5, data_format="channels_last", **kwargs): super(VisemeNet, self).__init__(**kwargs) stage1_rnn_hidden_size = 256 stage1_fc_mid_channels = 256 stage2_rnn_in_features = (audio_features + num_landmarks + stage1_fc_mid_channels) * \ stage2_window_size // audio_window_size self.audio_window_size = audio_window_size self.stage2_window_size = stage2_window_size self.stage1_rnn = nn.RNN([ nn.LSTMCell(units=stage1_rnn_hidden_size, dropout=dropout_rate, name="stage1_rnn{}".format(i + 1)) for i in range(3) ]) self.lm_branch = VisemeDenseBranch( in_channels=(stage1_rnn_hidden_size + num_face_ids), out_channels_list=[stage1_fc_mid_channels, num_landmarks], data_format=data_format, name="lm_branch") self.ph_branch = VisemeDenseBranch( in_channels=(stage1_rnn_hidden_size + num_face_ids), out_channels_list=[stage1_fc_mid_channels, num_phonemes], data_format=data_format, name="ph_branch") self.cls_branch = VisemeRnnBranch( in_channels=stage2_rnn_in_features, out_channels_list=[256, 200, num_visemes], rnn_num_layers=1, dropout_rate=dropout_rate, data_format=data_format, name="cls_branch") self.reg_branch = VisemeRnnBranch( in_channels=stage2_rnn_in_features, out_channels_list=[256, 200, 100, num_visemes], rnn_num_layers=3, dropout_rate=dropout_rate, data_format=data_format, name="reg_branch") self.jali_branch = VisemeRnnBranch(in_channels=stage2_rnn_in_features, out_channels_list=[128, 200, 2], rnn_num_layers=3, dropout_rate=dropout_rate, data_format=data_format, name="jali_branch")
def __init__(self, units): super(MyLSTM, self).__init__() # 初始化[c0,h0] self.state0 = [ tf.zeros([batchSize, units]), tf.zeros([batchSize, units]) ] self.state1 = [ tf.zeros([batchSize, units]), tf.zeros([batchSize, units]) ] # [b,200] => [b,200,100] self.embedding = layers.Embedding(input_dim=total_words, input_length=max_review_len, output_dim=embedding_len) self.lstm_cell0 = layers.LSTMCell(units=units, dropout=0.5) self.lstm_cell1 = layers.LSTMCell(units=units, dropout=0.5) self.outlayer1 = layers.Dense(32) self.outlayer2 = layers.Dense(1)
def __init__(self, input_dim, output_dim, model_size="small", bias=False, act=tf.nn.relu, concat=False): if model_size == "small": hidden_dim = self.hidden_dim = 128 elif model_size == "big": hidden_dim = self.hidden_dim = 256 else: raise AttributeError("model_size should be small or big") super(SeqAggregator, self).__init__(input_dim, output_dim, hidden_dim, bias, act, concat) self.cell = layers.LSTMCell(self.hidden_dim)
def _build_encoder(self, encoder, layer_size, num_layers): if encoder is None: enc_rnn_cells = [ layers.LSTMCell(layer_size, name=("enc_lstm_%d" % i)) for (i, layer_size) in enumerate([layer_size] * num_layers) ] encoder = layers.RNN(enc_rnn_cells, return_state=True, return_sequences=True) return encoder
def __init__(self): super().__init__() self.embed_layer = layers.Embedding(10, 32, batch_input_shape=[None, None]) self.rnncell = layers.LSTMCell(64) self.rnn_layer = layers.RNN(self.rnncell, return_sequences=True) self.dense1 = layers.Dense(64, activation='relu') self.dense2 = layers.Dense(32, activation='relu') self.dense3 = layers.Dense(10)
def __init__(self, config): super(DecoderOf, self).__init__(name="trajectory_decoder") self.rnn_type = config.rnn_type # Linear embedding of the encoding resulting observed trajectories self.traj_xy_emb_dec = layers.Dense( config.emb_size, activation=config.activation_func, name='trajectory_position_embedding') # RNN cell # Condition for cell type if self.rnn_type == 'gru': # GRU cell self.dec_cell_traj = layers.GRUCell( config.dec_hidden_size, recurrent_initializer='glorot_uniform', dropout=config.dropout_rate, recurrent_dropout=config.dropout_rate, name='trajectory_decoder_cell_with_GRU') else: # LSTM cell self.dec_cell_traj = layers.LSTMCell( config.dec_hidden_size, recurrent_initializer='glorot_uniform', name='trajectory_decoder_cell_with_LSTM', dropout=config.dropout_rate, recurrent_dropout=config.dropout_rate) # RNN layer self.recurrentLayer = layers.RNN(self.dec_cell_traj, return_sequences=True, return_state=True) # Dropout layer self.dropout = layers.Dropout(config.dropout_rate, name="dropout_decoder_h") # Mapping from h to positions self.h_to_xy = layers.Dense(config.P, activation=tf.identity, name='h_to_xy') # Input layers # Position input dec_input_shape = (1, config.P) self.input_layer_pos = layers.Input(dec_input_shape, name="position") enc_last_state_shape = (config.dec_hidden_size) # Proposals for inital states self.input_layer_hid1 = layers.Input(enc_last_state_shape, name="initial_state_h") self.input_layer_hid2 = layers.Input(enc_last_state_shape, name="initial_state_c") self.out = self.call((self.input_layer_pos, (self.input_layer_hid1, self.input_layer_hid2))) # Call init again. This is a workaround for being able to use summary super(DecoderOf, self).__init__(inputs=[ self.input_layer_pos, self.input_layer_hid1, self.input_layer_hid2 ], outputs=self.out)
def __init__(self, units): super(MyRNN, self).__init__() # [b, 64],构建Cell初始化状态向量,重复使用 self.state0 = [tf.zeros([batchsz, units]), tf.zeros([batchsz, units])] self.state1 = [tf.zeros([batchsz, units]), tf.zeros([batchsz, units])] # 词向量编码 [b, 80] => [b, 80, 100] self.embedding = layers.Embedding(total_words, embedding_len, input_length=max_review_len) # 构建2个Cell self.rnn_cell0 = layers.LSTMCell(units, dropout=0.5) self.rnn_cell1 = layers.LSTMCell(units, dropout=0.5) # 构建分类网络,用于将CELL的输出特征进行分类,2分类 # [b, 80, 100] => [b, 64] => [b, 1] self.outlayer = Sequential([ layers.Dense(units), layers.Dropout(rate=0.5), layers.ReLU(), layers.Dense(1) ])
def __init__(self, action_space): super(RecurrentQNetwork, self).__init__() self.action_space = action_space self.input_layer = kl.Dense(128, activation='relu', kernel_initializer='he_normal') self.lstm = kl.LSTMCell(128) self.output_layer = kl.Dense(action_space, kernel_initializer='he_normal')
def build_model_LSTM(units,input_dim,output_size): lstm_layer = layers.RNN( layers.LSTMCell(units), input_shape=(None, input_dim) ) model = keras.models.Sequential( [ lstm_layer, layers.BatchNormalization(), layers.Dense(output_size), ] ) return model
def __init__(self, units_layer1, units_layer2, embedding_len, BatchSize, MAX_DICTIONARY=10000, MAX_SENTENCE_WORDS=80): ''' 从父类继承过来的初始化函数,用于初始化所有模型所需要的层的参数 :param units_layer1: 第一层RNN网络的隐层神经元个数 :param units_layer2: 第二层RNN网络的隐层神经元个数 :param embedding_len: 词嵌入层的维度 :param BatchSize: 数据集中所使用的Batchsize批次,用于初始化隐藏状态 c0 和 a0 :param MAX_DICTIONARY: 词典大小,用于初始化embedding层 :param MAX_SENTENCE_WORDS: 句子最大长度,用于初始化embedding层 ''' super(MyRNN, self).__init__() self.embedding = layers.Embedding( MAX_DICTIONARY, embedding_len, input_length=MAX_SENTENCE_WORDS ) #第一个参数表示你的数据集中各不相同的词汇有多少,后面表示你要把这些词汇从稀疏连接投影到什么维度的密集连接上来 # 构建一个RNNCELL 使用LSTM来构建 self.cnn_cell0 = layers.LSTMCell(units_layer1, dropout=0.2) self.state0_layer1 = [ tf.zeros([BatchSize, units_layer1]), tf.zeros([BatchSize, units_layer1]) ] # [c0,a0] self.cnn_cell1 = layers.LSTMCell(units_layer2, dropout=0.2) self.state0_layer2 = [ tf.zeros([BatchSize, units_layer2]), tf.zeros([BatchSize, units_layer2]) ] # [c0,a0] # self.rnn_cell0 = layers.SimpleRNNCell(units_layer1,dropout=0.2) # self.state0_layer1 = [tf.zeros([BatchSize,units_layer1])] # 初始化最开始的向量 # self.rnn_cell1 = layers.SimpleRNNCell(units_layer2,dropout=0.2) # self.state0_layer2 = [tf.zeros([BatchSize,units_layer2])] # 构建一个将units 投影为待输出的 FC self.fc = layers.Dense(1)