I am fairly new to TF and started to learn it with TF tutorials.
I have just simply copied the Swivel model from TF site, and try to run it but,
I am getting an error message:
Traceback (most recent call last):
File "C:\Users\jhan\Desktop\tensorflow prac\swivel\swivel.py", line 362, in tf.app.run()
File "C:\Users\jhan\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\platform\app.py", line 44, in run _sys.exit(main(_sys.argv[:1] + flags_passthrough))
File "C:\Users\jhan\Desktop\tensorflow prac\swivel\swivel.py", line 289, in main model = SwivelModel(FLAGS)
File "C:\Users\jhan\Desktop\tensorflow prac\swivel\swivel.py", line 257, in init l2_loss = tf.reduce_mean(tf.concat(axis=0, values=l2_losses), 0,
File "C:\Users\jhan\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1034, in concat name=name)
File "C:\Users\jhan\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 519, in _concat_v2 name=name)
File "C:\Users\jhan\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 546, in apply_op (input_name, op_type_name, len(values), num_attr.minimum))
ValueError: List argument 'values' to 'ConcatV2' Op with length 0 shorter than minimum length 2..
my code is:
class SwivelModel(object):
def __init__(self, config):
"""Construct graph for dmc."""
self._config = config
# Create paths to input data files
log('Reading model from: %s', config.input_base_path)
count_matrix_files = glob.glob(config.input_base_path + '/shard-*.pb')
row_sums_path = config.input_base_path + '/row_sums.txt'
col_sums_path = config.input_base_path + '/col_sums.txt'
# Read marginals
row_sums = read_marginals_file(row_sums_path)
col_sums = read_marginals_file(col_sums_path)
self.n_rows = len(row_sums)
self.n_cols = len(col_sums)
log('Matrix dim: (%d,%d) SubMatrix dim: (%d,%d)',
self.n_rows, self.n_cols, config.submatrix_rows, config.submatrix_cols)
self.n_submatrices = (self.n_rows * self.n_cols /
(config.submatrix_rows * config.submatrix_cols))
log('n_submatrices: %d', self.n_submatrices)
with tf.device('/cpu:0'):
# ===== CREATE VARIABLES ======
# Get input
global_row, global_col, count = count_matrix_input(
count_matrix_files, config.submatrix_rows, config.submatrix_cols)
# Embeddings
self.row_embedding = embeddings_with_init(
embedding_dim=config.embedding_size,
vocab_size=self.n_rows,
name='row_embedding')
self.col_embedding = embeddings_with_init(
embedding_dim=config.embedding_size,
vocab_size=self.n_cols,
name='col_embedding')
tf.summary.histogram('row_emb', self.row_embedding)
tf.summary.histogram('col_emb', self.col_embedding)
matrix_log_sum = math.log(np.sum(row_sums) + 1)
row_bias_init = [math.log(x + 1) for x in row_sums]
col_bias_init = [math.log(x + 1) for x in col_sums]
self.row_bias = tf.Variable(
row_bias_init, trainable=config.trainable_bias)
self.col_bias = tf.Variable(
col_bias_init, trainable=config.trainable_bias)
tf.summary.histogram('row_bias', self.row_bias)
tf.summary.histogram('col_bias', self.col_bias)
# Add optimizer
l2_losses = []
sigmoid_losses = []
self.global_step = tf.Variable(0, name='global_step')
opt = tf.train.AdagradOptimizer(config.learning_rate)
all_grads = []
devices = ['/gpu:%d' % i for i in range(FLAGS.num_gpus)] \
if FLAGS.num_gpus > 0 else get_available_gpus()
self.devices_number = len(devices)
with tf.variable_scope(tf.get_variable_scope()):
for dev in devices:
with tf.device(dev):
with tf.name_scope(dev[1:].replace(':', '_')):
# ===== CREATE GRAPH =====
# Fetch embeddings.
selected_row_embedding = tf.nn.embedding_lookup(
self.row_embedding, global_row)
selected_col_embedding = tf.nn.embedding_lookup(
self.col_embedding, global_col)
# Fetch biases.
selected_row_bias = tf.nn.embedding_lookup(
[self.row_bias], global_row)
selected_col_bias = tf.nn.embedding_lookup(
[self.col_bias], global_col)
# Multiply the row and column embeddings to generate predictions.
predictions = tf.matmul(
selected_row_embedding, selected_col_embedding,
transpose_b=True)
# These binary masks separate zero from non-zero values.
count_is_nonzero = tf.to_float(tf.cast(count, tf.bool))
count_is_zero = 1 - count_is_nonzero
objectives = count_is_nonzero * tf.log(count + 1e-30)
objectives -= tf.reshape(
selected_row_bias, [config.submatrix_rows, 1])
objectives -= selected_col_bias
objectives += matrix_log_sum
err = predictions - objectives
# The confidence function scales the L2 loss based on the raw
# co-occurrence count.
l2_confidence = (config.confidence_base +
config.confidence_scale * tf.pow(
count, config.confidence_exponent))
l2_loss = config.loss_multiplier * tf.reduce_sum(
0.5 * l2_confidence * err * err * count_is_nonzero)
l2_losses.append(tf.expand_dims(l2_loss, 0))
sigmoid_loss = config.loss_multiplier * tf.reduce_sum(
tf.nn.softplus(err) * count_is_zero)
sigmoid_losses.append(tf.expand_dims(sigmoid_loss, 0))
loss = l2_loss + sigmoid_loss
grads = opt.compute_gradients(loss)
all_grads.append(grads)
with tf.device('/cpu:0'):
# ===== MERGE LOSSES =====
l2_loss = tf.reduce_mean(tf.concat(axis=0, values=l2_losses), 0,
name="l2_loss")
sigmoid_loss = tf.reduce_mean(tf.concat(axis=0, values=sigmoid_losses), 0,
name="sigmoid_loss")
self.loss = l2_loss + sigmoid_loss
average = tf.train.ExponentialMovingAverage(0.8, self.global_step)
loss_average_op = average.apply((self.loss,))
tf.summary.scalar("l2_loss", l2_loss)
tf.summary.scalar("sigmoid_loss", sigmoid_loss)
tf.summary.scalar("loss", self.loss)
# Apply the gradients to adjust the shared variables.
apply_gradient_ops = []
for grads in all_grads:
apply_gradient_ops.append(opt.apply_gradients(
grads, global_step=self.global_step))
self.train_op = tf.group(loss_average_op, *apply_gradient_ops)
self.saver = tf.train.Saver(sharded=True)