本文整理匯總了Python中utils.batch_generator方法的典型用法代碼示例。如果您正苦於以下問題:Python utils.batch_generator方法的具體用法?Python utils.batch_generator怎麽用?Python utils.batch_generator使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類utils
的用法示例。
在下文中一共展示了utils.batch_generator方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _stochastic_gradient_descent
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import batch_generator [as 別名]
def _stochastic_gradient_descent(self, _data):
"""
Performs stochastic gradient descend optimization algorithm.
:param _data: array-like, shape = (n_samples, n_features)
:return:
"""
for iteration in range(1, self.n_epochs + 1):
idx = np.random.permutation(len(_data))
data = _data[idx]
for batch in batch_generator(self.batch_size, data):
if len(batch) < self.batch_size:
# Pad with zeros
pad = np.zeros((self.batch_size - batch.shape[0], batch.shape[1]), dtype=batch.dtype)
batch = np.vstack((batch, pad))
sess.run(tf.variables_initializer(self.random_variables)) # Need to re-sample from uniform distribution
sess.run([self.update_W, self.update_b, self.update_c],
feed_dict={self.visible_units_placeholder: batch})
if self.verbose:
error = self._compute_reconstruction_error(data)
print(">> Epoch %d finished \tRBM Reconstruction error %f" % (iteration, error))
示例2: train_model
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import batch_generator [as 別名]
def train_model(model, args, X_train, X_valid, y_train, y_valid):
"""
Train the model
"""
checkpoint = ModelCheckpoint('model-{epoch:03d}.h5',
monitor='val_loss',
verbose=0,
save_best_only=args.save_best_only,
mode='auto')
model.compile(loss='mean_squared_error', optimizer=Adam(lr=args.learning_rate))
model.fit_generator(batch_generator(args.data_dir, X_train, y_train, args.batch_size, True),
args.samples_per_epoch,
args.nb_epoch,
max_q_size=1,
validation_data=batch_generator(args.data_dir, X_valid, y_valid, args.batch_size, False),
nb_val_samples=len(X_valid),
callbacks=[checkpoint],
verbose=1)
示例3: _stochastic_gradient_descent
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import batch_generator [as 別名]
def _stochastic_gradient_descent(self, _data):
"""
Performs stochastic gradient descend optimization algorithm.
:param _data: array-like, shape = (n_samples, n_features)
:return:
"""
accum_delta_W = np.zeros(self.W.shape)
accum_delta_b = np.zeros(self.b.shape)
accum_delta_c = np.zeros(self.c.shape)
for iteration in range(1, self.n_epochs + 1):
idx = np.random.permutation(len(_data))
data = _data[idx]
for batch in batch_generator(self.batch_size, data):
accum_delta_W[:] = .0
accum_delta_b[:] = .0
accum_delta_c[:] = .0
for sample in batch:
delta_W, delta_b, delta_c = self._contrastive_divergence(sample)
accum_delta_W += delta_W
accum_delta_b += delta_b
accum_delta_c += delta_c
self.W += self.learning_rate * (accum_delta_W / self.batch_size)
self.b += self.learning_rate * (accum_delta_b / self.batch_size)
self.c += self.learning_rate * (accum_delta_c / self.batch_size)
if self.verbose:
error = self._compute_reconstruction_error(data)
print(">> Epoch %d finished \tRBM Reconstruction error %f" % (iteration, error))
示例4: build_attention_model
# 需要導入模塊: import utils [as 別名]
# 或者: from utils import batch_generator [as 別名]
def build_attention_model():
# Different placeholders
with tf.name_scope('Inputs'):
batch_ph = tf.placeholder(tf.int32, [None, SEQUENCE_LENGTH], name='batch_ph')
target_ph = tf.placeholder(tf.float32, [None], name='target_ph')
seq_len_ph = tf.placeholder(tf.int32, [None], name='seq_len_ph')
keep_prob_ph = tf.placeholder(tf.float32, name='keep_prob_ph')
# Embedding layer
with tf.name_scope('Embedding_layer'):
embeddings_var = tf.Variable(tf.random_uniform([vocabulary_size, EMBEDDING_DIM], -1.0, 1.0), trainable=True)
tf.summary.histogram('embeddings_var', embeddings_var)
batch_embedded = tf.nn.embedding_lookup(embeddings_var, batch_ph)
# (Bi-)RNN layer(-s)
rnn_outputs, _ = bi_rnn(GRUCell(HIDDEN_UNITS), GRUCell(HIDDEN_UNITS),
inputs=batch_embedded, sequence_length=seq_len_ph, dtype=tf.float32)
tf.summary.histogram('RNN_outputs', rnn_outputs)
# Attention layer
with tf.name_scope('Attention_layer'):
attention_output, alphas = attention(rnn_outputs, ATTENTION_UNITS, return_alphas=True)
tf.summary.histogram('alphas', alphas)
# Dropout
drop = tf.nn.dropout(attention_output, keep_prob_ph)
# Fully connected layer
with tf.name_scope('Fully_connected_layer'):
W = tf.Variable(
tf.truncated_normal([HIDDEN_UNITS * 2, 1], stddev=0.1)) # Hidden size is multiplied by 2 for Bi-RNN
b = tf.Variable(tf.constant(0., shape=[1]))
y_hat = tf.nn.xw_plus_b(drop, W, b)
y_hat = tf.squeeze(y_hat)
tf.summary.histogram('W', W)
with tf.name_scope('Metrics'):
# Cross-entropy loss and optimizer initialization
loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=y_hat, labels=target_ph))
tf.summary.scalar('loss', loss)
optimizer = tf.train.AdamOptimizer(learning_rate=1e-3).minimize(loss)
# Accuracy metric
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.round(tf.sigmoid(y_hat)), target_ph), tf.float32))
tf.summary.scalar('accuracy', accuracy)
merged = tf.summary.merge_all()
# Batch generators
train_batch_generator = batch_generator(X_train, y_train, BATCH_SIZE)
test_batch_generator = batch_generator(X_test, y_test, BATCH_SIZE)
session_conf = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))
saver = tf.train.Saver()
return batch_ph, target_ph, seq_len_ph, keep_prob_ph, alphas, loss, accuracy, optimizer, merged, \
train_batch_generator, test_batch_generator, session_conf, saver