我们从Python开源项目中,提取了以下32个代码示例,用于说明如何使用tensorflow.sparse_placeholder()。
def get_edit_distance(hyp_arr, truth_arr, normalize, level): ''' calculate edit distance This is very universal, both for cha-level and phn-level ''' graph = tf.Graph() with graph.as_default(): truth = tf.sparse_placeholder(tf.int32) hyp = tf.sparse_placeholder(tf.int32) editDist = tf.reduce_sum(tf.edit_distance(hyp, truth, normalize=normalize)) with tf.Session(graph=graph) as session: truthTest = list_to_sparse_tensor(truth_arr, level) hypTest = list_to_sparse_tensor(hyp_arr, level) feedDict = {truth: truthTest, hyp: hypTest} dist = session.run(editDist, feed_dict=feedDict) return dist
def __init__(self, input_size, batch_size, data_generator_creator, max_steps=None): super().__init__(input_size) self.batch_size = batch_size self.data_generator_creator = data_generator_creator self.steps_left = max_steps with tf.device("/cpu:0"): # Define input and label placeholders # inputs is of dimension [batch_size, max_time, input_size] self.inputs = tf.placeholder(tf.float32, [batch_size, None, input_size], name='inputs') self.sequence_lengths = tf.placeholder(tf.int32, [batch_size], name='sequence_lengths') self.labels = tf.sparse_placeholder(tf.int32, name='labels') # Queue for inputs and labels self.queue = tf.FIFOQueue(dtypes=[tf.float32, tf.int32, tf.string], capacity=100) # queues do not support sparse tensors yet, we need to serialize... serialized_labels = tf.serialize_many_sparse(self.labels) self.enqueue_op = self.queue.enqueue([self.inputs, self.sequence_lengths, serialized_labels])
def __init__(self, lr, vocabulary_size=1295, mparams=None): super(Model, self).__init__() self._params = ModelParams(vocabulary_size=vocabulary_size) self.lr = lr self.learning_rate = tf.placeholder(tf.float32, shape=[]) self.images = tf.placeholder(dtype=tf.float32, shape=[None, 32, 100], name='images') self.seqs_length = tf.placeholder(dtype=tf.int32, shape=[None], name='seqs_length') self.targets = tf.sparse_placeholder(tf.int32, name='targets') self.ks = [3, 3, 3, 3, 3, 3, 2] self.ps = [1, 1, 1, 1, 1, 1, 0] self.nm = [64, 128, 256, 256, 512, 512, 512] self.nh = [256, 256] self.is_training = tf.placeholder(dtype=tf.bool, shape=[]) self._build_training() # Saver with tf.device('/cpu:0'): self.saver = tf.train.Saver(tf.global_variables())
def __init__(self, vocabulary_size=1295, mparams=None): super(Model, self).__init__() self._params = ModelParams(vocabulary_size=vocabulary_size) self.learning_rate = 1e-5 self.images = tf.placeholder(dtype=tf.float32, shape=[None, None, 224, 224, 3], name='images') self.seqs_length = tf.placeholder(dtype=tf.int32, shape=[None], name='seqs_length') self.targets = tf.sparse_placeholder(tf.int32, name='targets') self._build_training() # # Saver # with tf.device('/cpu:0'): # self.saver = tf.train.Saver(tf.all_variables())
def sparse_tuple_from(sequences, dtype=np.int32): """????list????????????????tensorflow?tf.sparse_placeholder???? Args: sequences: ????? Returns: ???????tensorflow?tf.sparse_placeholder??? """ indices = [] values = [] for n, seq in enumerate(sequences): indices.extend(zip([n]*len(seq), range(len(seq)))) values.extend(seq) indices = np.asarray(indices, dtype=np.int64) values = np.asarray(values, dtype=dtype) shape = np.asarray([len(sequences), np.asarray(indices).max(0)[1]+1], dtype=np.int64) return indices, values, shape
def placeholder(shape=None, ndim=None, dtype=_FLOATX, sparse=False, name=None): '''Instantiates a placeholder. # Arguments shape: shape of the placeholder (integer tuple, may include None entries). ndim: number of axes of the tensor. At least one of {`shape`, `ndim`} must be specified. If both are specified, `shape` is used. dtype: placeholder type. name: optional name string for the placeholder. # Returns Placeholder tensor instance. ''' if not shape: if ndim: shape = tuple([None for _ in range(ndim)]) if sparse: x = tf.sparse_placeholder(dtype, name=name) x._dims = len(shape) else: x = tf.placeholder(dtype, shape=shape, name=name) x._keras_shape = shape x._uses_learning_phase = False return x
def __init__(self, mode): self.mode = mode # image self.inputs = tf.placeholder(tf.float32, [None, FLAGS.image_height, FLAGS.image_width, FLAGS.image_channel]) # SparseTensor required by ctc_loss op self.labels = tf.sparse_placeholder(tf.int32) # 1d array of size [batch_size] self.seq_len = tf.placeholder(tf.int32, [None]) # l2 self._extra_train_ops = []
def testMalformedSparseFeatures(self): tensors = { 'a': tf.sparse_placeholder(tf.int64), } # Invalid indices. schema = self.toSchema({ 'a': tf.SparseFeature('idx', 'val', tf.float32, 10) }) instances = [{'a': ([-1, 2], [1.0, 2.0])}] with self.assertRaisesRegexp( ValueError, 'has index .* out of range'): _ = impl_helper.make_feed_dict(tensors, schema, instances) instances = [{'a': ([11, 1], [1.0, 2.0])}] with self.assertRaisesRegexp( ValueError, 'has index .* out of range'): _ = impl_helper.make_feed_dict(tensors, schema, instances) # Indices and values of different lengths. schema = self.toSchema({ 'a': tf.SparseFeature('idx', 'val', tf.float32, 10) }) instances = [{'a': ([1, 2], [1])}] with self.assertRaisesRegexp( ValueError, 'indices and values of different lengths'): _ = impl_helper.make_feed_dict(tensors, schema, instances) # Tuple of the wrong length. instances = [{'a': ([1], [2], [3])}] with self.assertRaisesRegexp( ValueError, 'too many values to unpack'): _ = impl_helper.make_feed_dict(tensors, schema, instances)
def as_batched_placeholder(self, column): return tf.sparse_placeholder( column.domain.dtype, [None] + column.tf_shape().as_list())
def test_infer_column_schema_from_tensor(self): dense = tf.constant([[1., 2.], [3., 4.]], dtype=tf.float32, shape=[2, 2]) column_schema = sch.infer_column_schema_from_tensor(dense) expected_column_schema = sch.ColumnSchema( tf.float32, [2], sch.FixedColumnRepresentation()) self.assertEqual(expected_column_schema, column_schema) varlen = tf.sparse_placeholder(tf.string) column_schema = sch.infer_column_schema_from_tensor(varlen) expected_column_schema = sch.ColumnSchema( tf.string, [None], sch.ListColumnRepresentation()) self.assertEqual(expected_column_schema, column_schema)
def test_sparse_roundtrip(self): export_path = os.path.join(tempfile.mkdtemp(), 'export') with tf.Graph().as_default(): with tf.Session().as_default() as session: input_float = tf.sparse_placeholder(tf.float32) output = input_float / 5.0 inputs = {'input': input_float} outputs = {'output': output} saved_transform_io.write_saved_transform_from_session( session, inputs, outputs, export_path) with tf.Graph().as_default(): with tf.Session().as_default() as session: indices = np.array([[3, 2, 0], [4, 5, 1]], dtype=np.int64) values = np.array([1.0, 2.0], dtype=np.float32) shape = np.array([7, 9, 2], dtype=np.int64) input_sparse = tf.SparseTensor( indices=indices, values=values, dense_shape=shape) # Using a computed input gives confidence that the graphs are fused inputs = {'input': input_sparse * 10} outputs = saved_transform_io.apply_saved_transform(export_path, inputs) output_sparse = outputs['output'] self.assertTrue(isinstance(output_sparse, tf.SparseTensor)) result = session.run(output_sparse) # indices and shape unchanged; values divided by 2 self.assertEqual(indices.tolist(), result.indices.tolist()) self.assertEqual([2.0, 4.0], result.values.tolist()) self.assertEqual(shape.tolist(), result.dense_shape.tolist())
def test_edit_distance(): graph = tf.Graph() with graph.as_default(): truth = tf.sparse_placeholder(tf.int32) hyp = tf.sparse_placeholder(tf.int32) editDist = tf.edit_distance(hyp, truth, normalize=False) with tf.Session(graph=graph) as session: truthTest = sparse_tensor_feed([[0,1,2], [0,1,2,3,4]]) hypTest = sparse_tensor_feed([[3,4,5], [0,1,2,2]]) feedDict = {truth: truthTest, hyp: hypTest} dist = session.run([editDist], feed_dict=feedDict) print(dist)
def placeholder(shape=None, ndim=None, dtype=None, sparse=False, name=None): """Instantiates a placeholder tensor and returns it. # Arguments shape: Shape of the placeholder (integer tuple, may include `None` entries). ndim: Number of axes of the tensor. At least one of {`shape`, `ndim`} must be specified. If both are specified, `shape` is used. dtype: Placeholder type. name: Optional name string for the placeholder. # Returns Tensor instance (with Keras metadata included). # Examples ```python >>> from keras import backend as K >>> input_ph = K.placeholder(shape=(2, 4, 5)) >>> input_ph._keras_shape (2, 4, 5) >>> input_ph <tf.Tensor 'Placeholder_4:0' shape=(2, 4, 5) dtype=float32>
""" if dtype is None: dtype = floatx() if not shape: if ndim: shape = tuple([None for _ in range(ndim)]) if sparse: x = tf.sparse_placeholder(dtype, name=name) x._dims = len(shape) else: x = tf.placeholder(dtype, shape=shape, name=name) x._keras_shape = shape x._uses_learning_phase = False return x
```
def get_edit_distance(hyp_arr,truth_arr): ''' calculate edit distance ''' graph = tf.Graph() with graph.as_default(): truth = tf.sparse_placeholder(tf.int32) hyp = tf.sparse_placeholder(tf.int32) editDist = tf.edit_distance(hyp, truth, normalize=True) with tf.Session(graph=graph) as session: truthTest = list_to_sparse_tensor(truth_arr) hypTest = list_to_sparse_tensor(hyp_arr) feedDict = {truth: truthTest, hyp: hypTest} dist = session.run(editDist, feed_dict=feedDict) return dist
def placeholder(shape=None, ndim=None, dtype=None, sparse=False, name=None): '''Instantiates a placeholder tensor and returns it. # Arguments shape: Shape of the placeholder (integer tuple, may include `None` entries). ndim: Number of axes of the tensor. At least one of {`shape`, `ndim`} must be specified. If both are specified, `shape` is used. dtype: Placeholder type. name: Optional name string for the placeholder. # Returns Tensor instance (with Keras metadata included). # Examples ```python >>> from keras import backend as K >>> input_ph = K.placeholder(shape=(2, 4, 5)) >>> input_ph._keras_shape (2, 4, 5) >>> input_ph <tf.Tensor 'Placeholder_4:0' shape=(2, 4, 5) dtype=float32>
''' if dtype is None: dtype = floatx() if not shape: if ndim: shape = tuple([None for _ in range(ndim)]) if sparse: x = tf.sparse_placeholder(dtype, name=name) x._dims = len(shape) else: x = tf.placeholder(dtype, shape=shape, name=name) x._keras_shape = shape x._uses_learning_phase = False return x
def get_edit_distance(hyp_arr,truth_arr,mode='train'): ''' calculate edit distance ''' graph = tf.Graph() with graph.as_default(): truth = tf.sparse_placeholder(tf.int32) hyp = tf.sparse_placeholder(tf.int32) editDist = tf.edit_distance(hyp, truth, normalize=True) with tf.Session(graph=graph) as session: truthTest = list_to_sparse_tensor(truth_arr, mode) hypTest = list_to_sparse_tensor(hyp_arr, mode) feedDict = {truth: truthTest, hyp: hypTest} dist = session.run(editDist, feed_dict=feedDict) return dist
def train_targets(self) -> tf.Tensor: return tf.sparse_placeholder(tf.int32, name="targets")
def placeholder(shape=None, ndim=None, dtype=None, sparse=False, name=None): """Instantiates a placeholder tensor and returns it. # Arguments shape: Shape of the placeholder (integer tuple, may include `None` entries). ndim: Number of axes of the tensor. At least one of {`shape`, `ndim`} must be specified. If both are specified, `shape` is used. dtype: Placeholder type. sparse: Boolean, whether the placeholder should have a sparse type. name: Optional name string for the placeholder. # Returns Tensor instance (with Keras metadata included). # Examples ```python >>> from keras import backend as K >>> input_ph = K.placeholder(shape=(2, 4, 5)) >>> input_ph._keras_shape (2, 4, 5) >>> input_ph <tf.Tensor 'Placeholder_4:0' shape=(2, 4, 5) dtype=float32>
""" if dtype is None: dtype = floatx() if not shape: if ndim: shape = tuple([None for _ in range(ndim)]) if sparse: x = tf.sparse_placeholder(dtype, shape=shape, name=name) else: x = tf.placeholder(dtype, shape=shape, name=name) x._keras_shape = shape x._uses_learning_phase = False return x
def __init__(self, input_dim=None, output_dim=1, init_path=None, opt_algo='gd', learning_rate=1e-2, l2_weight=0, random_seed=None): Model.__init__(self) init_vars = [('w', [input_dim, output_dim], 'xavier', dtype), ('b', [output_dim], 'zero', dtype)] self.graph = tf.Graph() with self.graph.as_default(): if random_seed is not None: tf.set_random_seed(random_seed) self.X = tf.sparse_placeholder(dtype) self.y = tf.placeholder(dtype) self.vars = utils.init_var_map(init_vars, init_path) w = self.vars['w'] b = self.vars['b'] xw = tf.sparse_tensor_dense_matmul(self.X, w) logits = tf.reshape(xw + b, [-1]) self.y_prob = tf.sigmoid(logits) self.loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(labels=self.y, logits=logits)) + \ l2_weight * tf.nn.l2_loss(xw) self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) config = tf.ConfigProto() config.gpu_options.allow_growth = True self.sess = tf.Session(config=config) tf.global_variables_initializer().run(session=self.sess)
def __init__(self, input_dim=None, output_dim=1, factor_order=10, init_path=None, opt_algo='gd', learning_rate=1e-2, l2_w=0, l2_v=0, random_seed=None): Model.__init__(self) init_vars = [('w', [input_dim, output_dim], 'xavier', dtype), ('v', [input_dim, factor_order], 'xavier', dtype), ('b', [output_dim], 'zero', dtype)] self.graph = tf.Graph() with self.graph.as_default(): if random_seed is not None: tf.set_random_seed(random_seed) self.X = tf.sparse_placeholder(dtype) self.y = tf.placeholder(dtype) self.vars = utils.init_var_map(init_vars, init_path) w = self.vars['w'] v = self.vars['v'] b = self.vars['b'] X_square = tf.SparseTensor(self.X.indices, tf.square(self.X.values), tf.to_int64(tf.shape(self.X))) xv = tf.square(tf.sparse_tensor_dense_matmul(self.X, v)) p = 0.5 * tf.reshape( tf.reduce_sum(xv - tf.sparse_tensor_dense_matmul(X_square, tf.square(v)), 1), [-1, output_dim]) xw = tf.sparse_tensor_dense_matmul(self.X, w) logits = tf.reshape(xw + b + p, [-1]) self.y_prob = tf.sigmoid(logits) self.loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=logits, labels=self.y)) + \ l2_w * tf.nn.l2_loss(xw) + \ l2_v * tf.nn.l2_loss(xv) self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) config = tf.ConfigProto() config.gpu_options.allow_growth = True self.sess = tf.Session(config=config) tf.global_variables_initializer().run(session=self.sess)
def create_model(self): # Placeholders # [batch size, step, features] self.input_data = tf.placeholder(tf.float32, [None, None, self.args.num_features], name='wave_input') self.targets = tf.sparse_placeholder(tf.int32, name='target') self.seq_len = tf.placeholder(tf.int32, [None], name='sequence_length') skip = 0 ''' Construct of a stack of dilated causal convolutional layers ''' # First non-causal convolution to inputs to expand feature dimension h = conv1d(self.input_data, self.args.num_hidden, filter_width=self.args.filter_width, name='conv_in', normalization=self.args.layer_norm, activation=tf.nn.tanh) # As many as number of blocks, block means one total dilated convolution layers for blocks in range(self.args.num_blocks): # Construction of dilation for dilated in range(self.args.num_wavenet_layers): # [1,2,4,8,16..] rate = 2**dilated h, s = res_block(h, self.args.num_hidden, rate, self.args.causal, self.args.filter_width, normalization=self.args.layer_norm, activation=self.args.dilated_activation, name='{}block_{}layer'.format(blocks+1, dilated+1)) skip += s # Make skip connections with tf.variable_scope('postprocessing'): # 1*1 convolution skip = conv1d(tf.nn.relu(skip), self.args.num_hidden, filter_width=self.args.skip_filter_width, activation=tf.nn.relu, normalization=self.args.layer_norm, name='conv_out1') hidden = conv1d(skip, self.args.num_hidden, filter_width=self.args.skip_filter_width, activation=tf.nn.relu, normalization=self.args.layer_norm, name='conv_out2') self.logits = conv1d(hidden, self.args.num_classes, filter_width=1, activation=None, normalization=self.args.layer_norm, name='conv_out3') self.probability = tf.nn.softmax(self.logits) # To calculate ctc, consider timemajor self.logits_reshaped = tf.transpose(self.logits, [1,0,2]) self.loss = tf.reduce_mean(tf.nn.ctc_loss(labels=self.targets, inputs=self.logits_reshaped, sequence_length=self.seq_len)) self.decoded, _ = tf.nn.ctc_greedy_decoder(self.logits_reshaped, self.seq_len) self.ler = tf.reduce_mean(tf.edit_distance(tf.cast(self.decoded[0], tf.int32), self.targets)) # When use tf.contrib.layers.layer_norm(batch_norm), update_ops are placed in tf.GraphKeys.UPDATE_OPS so they need to be added as a dependency to the train_op update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) #with tf.control_dependencies(update_ops): #self.train_op = tf.train.AdamOptimizer(self.args.learning_rate).minimize(self.loss) trainable_vr = tf.trainable_variables() for i in trainable_vr: print(i.name) optimizer = tf.train.AdamOptimizer(self.args.learning_rate) grad, vrbs = zip(*optimizer.compute_gradients(self.loss)) # clip_by_global_norm returns (list_clipped, global_norm), global_norm is sum of total l2 gradient(inputs) # If global_norm(sum of total norm) is greater than clipnorm(maxgrad), each element of input shrunk as ratio of global norm # Right way of gradient clipping # Automatically ignore None gradient grads, _ = tf.clip_by_global_norm(grad, self.args.maxgrad) with tf.control_dependencies(update_ops): self.train_op = optimizer.apply_gradients(zip(grads, vrbs)) self.saver = tf.train.Saver()
def _run_test_als(self, use_factors_weights_cache): with self.test_session(): col_init = np.random.rand(7, 3) als_model = tf.contrib.factorization.WALSModel( 5, 7, 3, col_init=col_init, row_weights=None, col_weights=None, use_factors_weights_cache=use_factors_weights_cache) als_model.initialize_op.run() als_model.worker_init.run() als_model.row_update_prep_gramian_op.run() als_model.initialize_row_update_op.run() process_input_op = als_model.update_row_factors(self._wals_inputs)[1] process_input_op.run() row_factors1 = [x.eval() for x in als_model.row_factors] wals_model = tf.contrib.factorization.WALSModel( 5, 7, 3, col_init=col_init, row_weights=0, col_weights=0, use_factors_weights_cache=use_factors_weights_cache) wals_model.initialize_op.run() wals_model.worker_init.run() wals_model.row_update_prep_gramian_op.run() wals_model.initialize_row_update_op.run() process_input_op = wals_model.update_row_factors(self._wals_inputs)[1] process_input_op.run() row_factors2 = [x.eval() for x in wals_model.row_factors] for r1, r2 in zip(row_factors1, row_factors2): self.assertAllClose(r1, r2, atol=1e-3) # Here we test partial column updates. sp_c = np_matrix_to_tf_sparse(INPUT_MATRIX, col_slices=[2, 0], shuffle=True).eval() sp_feeder = tf.sparse_placeholder(tf.float32) feed_dict = {sp_feeder: sp_c} als_model.col_update_prep_gramian_op.run() als_model.initialize_col_update_op.run() process_input_op = als_model.update_col_factors(sp_input=sp_feeder)[1] process_input_op.run(feed_dict=feed_dict) col_factors1 = [x.eval() for x in als_model.col_factors] feed_dict = {sp_feeder: sp_c} wals_model.col_update_prep_gramian_op.run() wals_model.initialize_col_update_op.run() process_input_op = wals_model.update_col_factors(sp_input=sp_feeder)[1] process_input_op.run(feed_dict=feed_dict) col_factors2 = [x.eval() for x in wals_model.col_factors] for c1, c2 in zip(col_factors1, col_factors2): self.assertAllClose(c1, c2, rtol=5e-3, atol=1e-2)
def _run_test_als_transposed(self, use_factors_weights_cache): with self.test_session(): col_init = np.random.rand(7, 3) als_model = tf.contrib.factorization.WALSModel( 5, 7, 3, col_init=col_init, row_weights=None, col_weights=None, use_factors_weights_cache=use_factors_weights_cache) als_model.initialize_op.run() als_model.worker_init.run() wals_model = tf.contrib.factorization.WALSModel( 5, 7, 3, col_init=col_init, row_weights=[0] * 5, col_weights=[0] * 7, use_factors_weights_cache=use_factors_weights_cache) wals_model.initialize_op.run() wals_model.worker_init.run() sp_feeder = tf.sparse_placeholder(tf.float32) # Here test partial row update with identical inputs but with transposed # input for als. sp_r_t = np_matrix_to_tf_sparse(INPUT_MATRIX, [3, 1], transpose=True).eval() sp_r = np_matrix_to_tf_sparse(INPUT_MATRIX, [3, 1]).eval() feed_dict = {sp_feeder: sp_r_t} als_model.row_update_prep_gramian_op.run() als_model.initialize_row_update_op.run() process_input_op = als_model.update_row_factors(sp_input=sp_feeder, transpose_input=True)[1] process_input_op.run(feed_dict=feed_dict) # Only updated row 1 and row 3, so only compare these rows since others # have randomly initialized values. row_factors1 = [als_model.row_factors[0].eval()[1], als_model.row_factors[0].eval()[3]] feed_dict = {sp_feeder: sp_r} wals_model.row_update_prep_gramian_op.run() wals_model.initialize_row_update_op.run() process_input_op = wals_model.update_row_factors(sp_input=sp_feeder)[1] process_input_op.run(feed_dict=feed_dict) # Only updated row 1 and row 3, so only compare these rows since others # have randomly initialized values. row_factors2 = [wals_model.row_factors[0].eval()[1], wals_model.row_factors[0].eval()[3]] for r1, r2 in zip(row_factors1, row_factors2): self.assertAllClose(r1, r2, atol=1e-3)
def get_train_model(): # Has size [batch_size, max_stepsize, num_features], but the # batch_size and max_stepsize can vary along each step # features = convolutional_layers() # print features.get_shape() inputs = tf.placeholder(tf.float32, [None, None, common.OUTPUT_SHAPE[0]]) # Here we use sparse_placeholder that will generate a # SparseTensor required by ctc_loss op. targets = tf.sparse_placeholder(tf.int32) # 1d array of size [batch_size] seq_len = tf.placeholder(tf.int32, [None]) # Defining the cell # Can be: # tf.nn.rnn_cell.RNNCell # tf.nn.rnn_cell.GRUCell cell = tf.contrib.rnn.core_rnn_cell.LSTMCell(common.num_hidden, state_is_tuple=True) # Stacking rnn cells stack = tf.contrib.rnn.core_rnn_cell.MultiRNNCell([cell] * common.num_layers, state_is_tuple=True) # The second output is the last state and we will no use that outputs, _ = tf.nn.dynamic_rnn(cell, inputs, seq_len, dtype=tf.float32) shape = tf.shape(inputs) batch_s, max_timesteps = shape[0], shape[1] # Reshaping to apply the same weights over the timesteps outputs = tf.reshape(outputs, [-1, common.num_hidden]) # Truncated normal with mean 0 and stdev=0.1 # Tip: Try another initialization # see https://www.tensorflow.org/versions/r0.9/api_docs/python/contrib.layers.html#initializers W = tf.Variable(tf.truncated_normal([common.num_hidden, common.num_classes], stddev=0.1), name="W") # Zero initialization # Tip: Is tf.zeros_initializer the same? b = tf.Variable(tf.constant(0., shape=[common.num_classes]), name="b") # Doing the affine projection logits = tf.matmul(outputs, W) + b # Reshaping back to the original shape logits = tf.reshape(logits, [batch_s, -1, common.num_classes]) # Time major logits = tf.transpose(logits, (1, 0, 2)) return logits, inputs, targets, seq_len, W, b
def __init__(self, itm_cnt, usr_cnt, dim_hidden, n_time_step, learning_rate, grad_clip, emb_dim, lamda=0.2, initdelta=0.05,MF_paras=None,model_type="rnn",use_sparse_tensor=True,update_rule='adam'): """ Args: dim_itm_embed: (optional) Dimension of item embedding. dim_usr_embed: (optional) Dimension of user embedding. dim_hidden: (optional) Dimension of all hidden state. n_time_step: (optional) Time step size of LSTM. usr_cnt: (optional) The size of all users. itm_cnt: (optional) The size of all items. """ self.V_M = itm_cnt self.V_U = usr_cnt self.param=MF_paras self.H = dim_hidden self.T = n_time_step self.MF_paras=MF_paras self.grad_clip = grad_clip self.weight_initializer = tf.random_uniform_initializer(minval=-0.05, maxval=0.05) self.const_initializer = tf.constant_initializer(0.0) self.emb_initializer = tf.random_uniform_initializer(minval=-0.05, maxval=0.05) # Place holder for features and captions self.sparse_tensor=use_sparse_tensor if self.sparse_tensor: self.user_sparse_tensor= tf.sparse_placeholder(tf.float32) self.user_sequence = tf.sparse_tensor_to_dense(self.user_sparse_tensor) self.item_sparse_tensor= tf.sparse_placeholder(tf.float32) self.item_sequence = tf.sparse_tensor_to_dense(self.item_sparse_tensor) else: self.item_sequence = tf.placeholder(tf.float32, [None, self.T, self.V_U]) self.user_sequence = tf.placeholder(tf.float32, [None, self.T, self.V_M]) self.rating = tf.placeholder(tf.float32, [None,]) self.learning_rate = learning_rate self.emb_dim = emb_dim self.lamda = lamda # regularization parameters self.initdelta = initdelta self.u = tf.placeholder(tf.int32) self.i = tf.placeholder(tf.int32) self.paras_rnn=[] self.model_type=model_type self.update_rule = update_rule
def __init__(self, field_sizes=None, embed_size=10, layer_sizes=None, layer_acts=None, drop_out=None, embed_l2=None, layer_l2=None, init_path=None, opt_algo='gd', learning_rate=1e-2, random_seed=None): Model.__init__(self) init_vars = [] num_inputs = len(field_sizes) for i in range(num_inputs): init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) node_in = num_inputs * embed_size for i in range(len(layer_sizes)): init_vars.append(('w%d' % i, [node_in, layer_sizes[i]], 'xavier', dtype)) init_vars.append(('b%d' % i, [layer_sizes[i]], 'zero', dtype)) node_in = layer_sizes[i] self.graph = tf.Graph() with self.graph.as_default(): if random_seed is not None: tf.set_random_seed(random_seed) self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] self.y = tf.placeholder(dtype) self.keep_prob_train = 1 - np.array(drop_out) self.keep_prob_test = np.ones_like(drop_out) self.layer_keeps = tf.placeholder(dtype) self.vars = utils.init_var_map(init_vars, init_path) w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) l = xw for i in range(len(layer_sizes)): wi = self.vars['w%d' % i] bi = self.vars['b%d' % i] print(l.shape, wi.shape, bi.shape) l = tf.nn.dropout( utils.activate( tf.matmul(l, wi) + bi, layer_acts[i]), self.layer_keeps[i]) l = tf.squeeze(l) self.y_prob = tf.sigmoid(l) self.loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) if layer_l2 is not None: self.loss += embed_l2 * tf.nn.l2_loss(xw) for i in range(len(layer_sizes)): wi = self.vars['w%d' % i] self.loss += layer_l2[i] * tf.nn.l2_loss(wi) self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) config = tf.ConfigProto() config.gpu_options.allow_growth = True self.sess = tf.Session(config=config) tf.global_variables_initializer().run(session=self.sess)
def __init__(self, field_sizes=None, embed_size=10, filter_sizes=None, layer_acts=None, drop_out=None, init_path=None, opt_algo='gd', learning_rate=1e-2, random_seed=None): Model.__init__(self) init_vars = [] num_inputs = len(field_sizes) for i in range(num_inputs): init_vars.append(('embed_%d' % i, [field_sizes[i], embed_size], 'xavier', dtype)) init_vars.append(('f1', [embed_size, filter_sizes[0], 1, 2], 'xavier', dtype)) init_vars.append(('f2', [embed_size, filter_sizes[1], 2, 2], 'xavier', dtype)) init_vars.append(('w1', [2 * 3 * embed_size, 1], 'xavier', dtype)) init_vars.append(('b1', [1], 'zero', dtype)) self.graph = tf.Graph() with self.graph.as_default(): if random_seed is not None: tf.set_random_seed(random_seed) self.X = [tf.sparse_placeholder(dtype) for i in range(num_inputs)] self.y = tf.placeholder(dtype) self.keep_prob_train = 1 - np.array(drop_out) self.keep_prob_test = np.ones_like(drop_out) self.layer_keeps = tf.placeholder(dtype) self.vars = utils.init_var_map(init_vars, init_path) w0 = [self.vars['embed_%d' % i] for i in range(num_inputs)] xw = tf.concat([tf.sparse_tensor_dense_matmul(self.X[i], w0[i]) for i in range(num_inputs)], 1) l = xw l = tf.transpose(tf.reshape(l, [-1, num_inputs, embed_size, 1]), [0, 2, 1, 3]) f1 = self.vars['f1'] l = tf.nn.conv2d(l, f1, [1, 1, 1, 1], 'SAME') l = tf.transpose( utils.max_pool_4d( tf.transpose(l, [0, 1, 3, 2]), int(num_inputs / 2)), [0, 1, 3, 2]) f2 = self.vars['f2'] l = tf.nn.conv2d(l, f2, [1, 1, 1, 1], 'SAME') l = tf.transpose( utils.max_pool_4d( tf.transpose(l, [0, 1, 3, 2]), 3), [0, 1, 3, 2]) l = tf.nn.dropout( utils.activate( tf.reshape(l, [-1, embed_size * 3 * 2]), layer_acts[0]), self.layer_keeps[0]) w1 = self.vars['w1'] b1 = self.vars['b1'] l = tf.matmul(l, w1) + b1 l = tf.squeeze(l) self.y_prob = tf.sigmoid(l) self.loss = tf.reduce_mean( tf.nn.sigmoid_cross_entropy_with_logits(logits=l, labels=self.y)) self.optimizer = utils.get_optimizer(opt_algo, learning_rate, self.loss) config = tf.ConfigProto() config.gpu_options.allow_growth = True self.sess = tf.Session(config=config) tf.global_variables_initializer().run(session=self.sess)