本文整理汇总了Python中keras.layers.recurrent.GRU属性的典型用法代码示例。如果您正苦于以下问题:Python recurrent.GRU属性的具体用法?Python recurrent.GRU怎么用?Python recurrent.GRU使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类keras.layers.recurrent
的用法示例。
在下文中一共展示了recurrent.GRU属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_temporal_clf
# 需要导入模块: from keras.layers import recurrent [as 别名]
# 或者: from keras.layers.recurrent import GRU [as 别名]
def test_temporal_clf(self):
print('temporal classification data:')
(X_train, y_train), (X_test, y_test) = get_test_data(nb_train=1000, nb_test=200, input_shape=(5,10),
classification=True, nb_class=2)
print('X_train:', X_train.shape)
print('X_test:', X_test.shape)
print('y_train:', y_train.shape)
print('y_test:', y_test.shape)
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
model = Sequential()
model.add(GRU(X_train.shape[-1], y_train.shape[-1]))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adadelta')
history = model.fit(X_train, y_train, nb_epoch=12, batch_size=16, validation_data=(X_test, y_test), show_accuracy=True, verbose=2)
self.assertTrue(history.history['val_acc'][-1] > 0.9)
示例2: understand_variable_length_handle
# 需要导入模块: from keras.layers import recurrent [as 别名]
# 或者: from keras.layers.recurrent import GRU [as 别名]
def understand_variable_length_handle():
"""用来帮助理解如何用 recurrent layer 处理变长序列"""
model = Sequential()
model.add(GRU(input_dim=256, output_dim=256, return_sequences=True))
model.compile(loss='mean_squared_error', optimizer='sgd')
train_x = np.random.randn(100, 78, 256)
train_y = np.random.randn(100, 78, 256)
model.fit(train_x, train_y, verbose=0)
inz_1 = np.random.randn(1, 78, 256)
rez_1 = model.predict_proba(inz_1, verbose=0)
inz_2 = np.random.randn(1, 87, 256)
rez_2 = model.predict_proba(inz_2, verbose=0)
print()
print('=========== understand variable length =================')
print('With `return_sequence=True`')
print('Input shape is: {}, output shae is {}'.format(inz_1.shape, rez_1.shape))
print('Input shape is: {}, output shae is {}'.format(inz_2.shape, rez_2.shape))
print('====================== end =============================')
示例3: try_variable_length_train
# 需要导入模块: from keras.layers import recurrent [as 别名]
# 或者: from keras.layers.recurrent import GRU [as 别名]
def try_variable_length_train():
"""变长序列训练实验
实验失败,这样得到的 train_x 和 train_y 的 dtype 是 object 类型,
取其 shape 得到的是 (100,) ,这将导致训练出错
"""
model = Sequential()
model.add(GRU(input_dim=256, output_dim=256, return_sequences=True))
model.compile(loss='mean_squared_error', optimizer='sgd')
train_x = []
train_y = []
for i in range(100):
seq_length = np.random.randint(78, 87 + 1)
sequence = []
for _ in range(seq_length):
sequence.append([np.random.randn() for _ in range(256)])
train_x.append(np.array(sequence))
train_y.append(np.array(sequence))
train_x = np.array(train_x)
train_y = np.array(train_y)
model.fit(np.array(train_x), np.array(train_y))
示例4: gen_model
# 需要导入模块: from keras.layers import recurrent [as 别名]
# 或者: from keras.layers.recurrent import GRU [as 别名]
def gen_model(vocab_size=100, embedding_size=128, maxlen=100, output_size=6, hidden_layer_size=100, num_hidden_layers = 1, RNN_LAYER_TYPE="LSTM"):
RNN_CLASS = LSTM
if RNN_LAYER_TYPE == "GRU":
RNN_CLASS = GRU
logger.info("Parameters: vocab_size = %s, embedding_size = %s, maxlen = %s, output_size = %s, hidden_layer_size = %s, " %\
(vocab_size, embedding_size, maxlen, output_size, hidden_layer_size))
logger.info("Building Model")
model = Sequential()
logger.info("Init Model with vocab_size = %s, embedding_size = %s, maxlen = %s" % (vocab_size, embedding_size, maxlen))
model.add(Embedding(vocab_size, embedding_size, input_length=maxlen))
logger.info("Added Embedding Layer")
model.add(Dropout(0.5))
logger.info("Added Dropout Layer")
for i in xrange(num_hidden_layers):
model.add(RNN_CLASS(output_dim=hidden_layer_size, activation='sigmoid', inner_activation='hard_sigmoid', return_sequences=True))
logger.info("Added %s Layer" % RNN_LAYER_TYPE)
model.add(Dropout(0.5))
logger.info("Added Dropout Layer")
model.add(RNN_CLASS(output_dim=output_size, activation='sigmoid', inner_activation='hard_sigmoid', return_sequences=True))
logger.info("Added %s Layer" % RNN_LAYER_TYPE)
model.add(Dropout(0.5))
logger.info("Added Dropout Layer")
model.add(TimeDistributedDense(output_size, activation="softmax"))
logger.info("Added Dropout Layer")
logger.info("Created model with following config:\n%s" % json.dumps(model.get_config(), indent=4))
logger.info("Compiling model with optimizer %s" % optimizer)
start_time = time.time()
model.compile(loss='categorical_crossentropy', optimizer=optimizer)
total_time = time.time() - start_time
logger.info("Model compiled in %.4f seconds." % total_time)
return model
示例5: get_gru
# 需要导入模块: from keras.layers import recurrent [as 别名]
# 或者: from keras.layers.recurrent import GRU [as 别名]
def get_gru(units):
"""GRU(Gated Recurrent Unit)
Build GRU Model.
# Arguments
units: List(int), number of input, output and hidden units.
# Returns
model: Model, nn model.
"""
model = Sequential()
model.add(GRU(units[1], input_shape=(units[0], 1), return_sequences=True))
model.add(GRU(units[2]))
model.add(Dropout(0.2))
model.add(Dense(units[3], activation='sigmoid'))
return model
示例6: __init__
# 需要导入模块: from keras.layers import recurrent [as 别名]
# 或者: from keras.layers.recurrent import GRU [as 别名]
def __init__(self, input_shape, gru_size=10, tdd_size=4):
self.model = Sequential()
self.model.add(
GRU(gru_size, return_sequences=True, input_shape=input_shape))
if tdd_size is not None:
self.model.add(TimeDistributedDense(tdd_size))
self.model.add(Flatten())
self.model.add(Dense(1))
self.model.add(Activation('sigmoid'))
print('Compiling model...')
self.model.compile(optimizer='adam', loss='binary_crossentropy')
示例7: _buildDecoder
# 需要导入模块: from keras.layers import recurrent [as 别名]
# 或者: from keras.layers.recurrent import GRU [as 别名]
def _buildDecoder(self, z, latent_rep_size, max_length, charset_length):
h = Dense(latent_rep_size, name='latent_input', activation='relu')(z)
h = RepeatVector(max_length, name='repeat_vector')(h)
h = GRU(501, return_sequences=True, name='gru_1')(h)
h = GRU(501, return_sequences=True, name='gru_2')(h)
h = GRU(501, return_sequences=True, name='gru_3')(h)
return TimeDistributed(
Dense(charset_length, activation='softmax'), name='decoded_mean')(h)
示例8: test_gru
# 需要导入模块: from keras.layers import recurrent [as 别名]
# 或者: from keras.layers.recurrent import GRU [as 别名]
def test_gru(self):
_runner(recurrent.GRU)
示例9: test_temporal_reg
# 需要导入模块: from keras.layers import recurrent [as 别名]
# 或者: from keras.layers.recurrent import GRU [as 别名]
def test_temporal_reg(self):
print('temporal regression data:')
(X_train, y_train), (X_test, y_test) = get_test_data(nb_train=1000, nb_test=200, input_shape=(5, 10), output_shape=(2,),
classification=False)
print('X_train:', X_train.shape)
print('X_test:', X_test.shape)
print('y_train:', y_train.shape)
print('y_test:', y_test.shape)
model = Sequential()
model.add(GRU(X_train.shape[-1], y_train.shape[-1]))
model.compile(loss='hinge', optimizer='adam')
history = model.fit(X_train, y_train, nb_epoch=12, batch_size=16, validation_data=(X_test, y_test), verbose=2)
self.assertTrue(history.history['val_loss'][-1] < 0.8)
示例10: understand_return_sequence
# 需要导入模块: from keras.layers import recurrent [as 别名]
# 或者: from keras.layers.recurrent import GRU [as 别名]
def understand_return_sequence():
"""用来帮助理解 recurrent layer 中的 return_sequences 参数"""
model_1 = Sequential()
model_1.add(GRU(input_dim=256, output_dim=256, return_sequences=True))
model_1.compile(loss='mean_squared_error', optimizer='sgd')
train_x = np.random.randn(100, 78, 256)
train_y = np.random.randn(100, 78, 256)
model_1.fit(train_x, train_y, verbose=0)
model_2 = Sequential()
model_2.add(GRU(input_dim=256, output_dim=256, return_sequences=False))
model_2.compile(loss='mean_squared_error', optimizer='sgd')
train_x = np.random.randn(100, 78, 256)
train_y = np.random.randn(100, 256)
model_2.fit(train_x, train_y, verbose=0)
inz = np.random.randn(100, 78, 256)
rez_1 = model_1.predict_proba(inz, verbose=0)
rez_2 = model_2.predict_proba(inz, verbose=0)
print()
print('=========== understand return_sequence =================')
print('Input shape is: {}'.format(inz.shape))
print('Output shape of model with `return_sequences=True`: {}'.format(rez_1.shape))
print('Output shape of model with `return_sequences=False`: {}'.format(rez_2.shape))
print('====================== end =============================')
示例11: try_variable_length_train_in_batch
# 需要导入模块: from keras.layers import recurrent [as 别名]
# 或者: from keras.layers.recurrent import GRU [as 别名]
def try_variable_length_train_in_batch():
"""变长序列训练实验(2)"""
model = Sequential()
model.add(GRU(input_dim=256, output_dim=256, return_sequences=True))
model.compile(loss='mean_squared_error', optimizer='sgd')
# 分作两个 batch, 不同 batch 中的 sequence 长度不一样
seq_lens = [78, 87]
for i in range(2):
train_x = np.random.randn(20, seq_lens[i], 256)
train_y = np.random.randn(20, seq_lens[i], 256)
model.train_on_batch(train_x, train_y)
示例12: build_model
# 需要导入模块: from keras.layers import recurrent [as 别名]
# 或者: from keras.layers.recurrent import GRU [as 别名]
def build_model(input_size, seq_len, hidden_size):
"""建立一个 sequence to sequence 模型"""
model = Sequential()
model.add(GRU(input_dim=input_size, output_dim=hidden_size, return_sequences=False))
model.add(Dense(hidden_size, activation="relu"))
model.add(RepeatVector(seq_len))
model.add(GRU(hidden_size, return_sequences=True))
model.add(TimeDistributed(Dense(output_dim=input_size, activation="linear")))
model.compile(loss="mse", optimizer='adam')
return model
示例13: gen_model_brnn
# 需要导入模块: from keras.layers import recurrent [as 别名]
# 或者: from keras.layers.recurrent import GRU [as 别名]
def gen_model_brnn(vocab_size=100, embedding_size=128, maxlen=100, output_size=6, hidden_layer_size=100, num_hidden_layers = 1, RNN_LAYER_TYPE="LSTM"):
RNN_CLASS = LSTM
if RNN_LAYER_TYPE == "GRU":
RNN_CLASS = GRU
logger.info("Parameters: vocab_size = %s, embedding_size = %s, maxlen = %s, output_size = %s, hidden_layer_size = %s, " %\
(vocab_size, embedding_size, maxlen, output_size, hidden_layer_size))
logger.info("Building Graph model for Bidirectional RNN")
model = Graph()
model.add_input(name='input', input_shape=(maxlen,), dtype=int)
logger.info("Added Input node")
logger.info("Init Model with vocab_size = %s, embedding_size = %s, maxlen = %s" % (vocab_size, embedding_size, maxlen))
model.add_node(Embedding(vocab_size, embedding_size, input_length=maxlen), name='embedding', input='input')
logger.info("Added Embedding node")
model.add_node(Dropout(0.5), name="dropout_0", input="embedding")
logger.info("Added Dropout Node")
for i in xrange(num_hidden_layers):
last_dropout_name = "dropout_%s" % i
forward_name, backward_name, dropout_name = ["%s_%s" % (k, i + 1) for k in ["forward", "backward", "dropout"]]
model.add_node(RNN_CLASS(output_dim=hidden_layer_size, activation='sigmoid', inner_activation='hard_sigmoid', return_sequences=True), name=forward_name, input=last_dropout_name)
logger.info("Added %s forward node[%s]" % (RNN_LAYER_TYPE, i+1))
model.add_node(RNN_CLASS(output_dim=hidden_layer_size, activation='sigmoid', inner_activation='hard_sigmoid', return_sequences=True, go_backwards=True), name=backward_name, input=last_dropout_name)
logger.info("Added %s backward node[%s]" % (RNN_LAYER_TYPE, i+1))
model.add_node(Dropout(0.5), name=dropout_name, inputs=[forward_name, backward_name])
logger.info("Added Dropout node[%s]" % (i+1))
model.add_node(TimeDistributedDense(output_size, activation="softmax"), name="tdd", input=dropout_name)
logger.info("Added TimeDistributedDense node")
model.add_output(name="output", input="tdd")
logger.info("Added Output node")
logger.info("Created model with following config:\n%s" % model.get_config())
logger.info("Compiling model with optimizer %s" % optimizer)
start_time = time.time()
model.compile(optimizer, {"output": 'categorical_crossentropy'})
total_time = time.time() - start_time
logger.info("Model compiled in %.4f seconds." % total_time)
return model
示例14: gen_model_brnn_multitask
# 需要导入模块: from keras.layers import recurrent [as 别名]
# 或者: from keras.layers.recurrent import GRU [as 别名]
def gen_model_brnn_multitask(vocab_size=100, embedding_size=128, maxlen=100, output_size=[6, 96], hidden_layer_size=100, num_hidden_layers = 1, RNN_LAYER_TYPE="LSTM"):
RNN_CLASS = LSTM
if RNN_LAYER_TYPE == "GRU":
RNN_CLASS = GRU
logger.info("Parameters: vocab_size = %s, embedding_size = %s, maxlen = %s, output_size = %s, hidden_layer_size = %s, " %\
(vocab_size, embedding_size, maxlen, output_size, hidden_layer_size))
logger.info("Building Graph model for Bidirectional RNN")
model = Graph()
model.add_input(name='input', input_shape=(maxlen,), dtype=int)
logger.info("Added Input node")
logger.info("Init Model with vocab_size = %s, embedding_size = %s, maxlen = %s" % (vocab_size, embedding_size, maxlen))
model.add_node(Embedding(vocab_size, embedding_size, input_length=maxlen, mask_zero=True), name='embedding', input='input')
logger.info("Added Embedding node")
model.add_node(Dropout(0.5), name="dropout_0", input="embedding")
logger.info("Added Dropout Node")
for i in xrange(num_hidden_layers):
last_dropout_name = "dropout_%s" % i
forward_name, backward_name, dropout_name = ["%s_%s" % (k, i + 1) for k in ["forward", "backward", "dropout"]]
model.add_node(RNN_CLASS(output_dim=hidden_layer_size, activation='sigmoid', inner_activation='hard_sigmoid', return_sequences=True), name=forward_name, input=last_dropout_name)
logger.info("Added %s forward node[%s]" % (RNN_LAYER_TYPE, i+1))
model.add_node(RNN_CLASS(output_dim=hidden_layer_size, activation='sigmoid', inner_activation='hard_sigmoid', return_sequences=True, go_backwards=True), name=backward_name, input=last_dropout_name)
logger.info("Added %s backward node[%s]" % (RNN_LAYER_TYPE, i+1))
model.add_node(Dropout(0.5), name=dropout_name, inputs=[forward_name, backward_name])
logger.info("Added Dropout node[%s]" % (i+1))
output_names = []
for i, output_task_size in enumerate(output_size):
tdd_name, output_name = "tdd_%s" % i, "output_%s" % i
model.add_node(TimeDistributedDense(output_task_size, activation="softmax"), name=tdd_name, input=dropout_name)
logger.info("Added TimeDistributedDense node %s with output_size %s" % (i, output_task_size))
model.add_output(name=output_name, input=tdd_name)
output_names.append(output_name)
logger.info("Added Output node")
logger.info("Created model with following config:\n%s" % model.get_config())
logger.info("Compiling model with optimizer %s" % optimizer)
start_time = time.time()
model.compile(optimizer, {k: 'categorical_crossentropy' for k in output_names})
total_time = time.time() - start_time
logger.info("Model compiled in %.4f seconds." % total_time)
return model, output_names
示例15: build
# 需要导入模块: from keras.layers import recurrent [as 别名]
# 或者: from keras.layers.recurrent import GRU [as 别名]
def build(self, input_shape):
bs, input_length, input_dim = input_shape
self.controller_input_dim, self.controller_output_dim = controller_input_output_shape(
input_dim, self.units, self.m_depth, self.n_slots, self.shift_range, self.read_heads,
self.write_heads)
# Now that we've calculated the shape of the controller, we have add it to the layer/model.
if self.controller is None:
self.controller = Dense(
name = "controller",
activation = 'linear',
bias_initializer = 'zeros',
units = self.controller_output_dim,
input_shape = (bs, input_length, self.controller_input_dim))
self.controller.build(input_shape=(self.batch_size, input_length, self.controller_input_dim))
self.controller_with_state = False
# This is a fixed shift matrix
self.C = _circulant(self.n_slots, self.shift_range)
self.trainable_weights = self.controller.trainable_weights
# We need to declare the number of states we want to carry around.
# In our case the dimension seems to be 6 (LSTM) or 5 (GRU) or 4 (FF),
# see self.get_initial_states, those respond to:
# [old_ntm_output] + [init_M, init_wr, init_ww] + [init_h] (LSMT and GRU) + [(init_c] (LSTM only))
# old_ntm_output does not make sense in our world, but is required by the definition of the step function we
# intend to use.
# WARNING: What self.state_spec does is only poorly understood,
# I only copied it from keras/recurrent.py.
self.states = [None, None, None, None]
self.state_spec = [InputSpec(shape=(None, self.output_dim)), # old_ntm_output
InputSpec(shape=(None, self.n_slots, self.m_depth)), # Memory
InputSpec(shape=(None, self.read_heads, self.n_slots)), # weights_read
InputSpec(shape=(None, self.write_heads, self.n_slots))] # weights_write
super(NeuralTuringMachine, self).build(input_shape)