gpt4 book ai didi

tensorflow - 值错误 : List argument 'values' to 'ConcatV2' Op with length 0 shorter than minimum length 2

转载 作者:行者123 更新时间:2023-12-02 03:05:05 33 4
gpt4 key购买 nike

我是 TF 的新手,并开始通过 TF 教程学习它。我只是简单地从 TF 站点复制了 Swivel 模型,并尝试运行它,但是,我收到一条错误消息:
回溯(最近调用最后):

文件“C:\Users\jhan\Desktop\tensorflow prac\swivel\swivel.py”,第 362 行,位于 tf.app.run()

文件“C:\Users\jhan\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\platform\app.py”,第 44 行,在运行中 _sys.exit(main(_sys.argv[:1] + flags_passthrough))

文件“C:\Users\jhan\Desktop\tensorflow prac\swivel\swivel.py”,第 289 行,在 main 模型 = SwivelModel(FLAGS)

init 中的文件“C:\Users\jhan\Desktop\tensorflow prac\swivel\swivel.py”,第 257 行 l2_loss = tf.reduce_mean(tf.concat(axis=0, values=l2_losses), 0,

文件“C:\Users\jhan\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\array_ops.py”,第 1034 行,concat 名字=名字)

文件“C:\Users\jhan\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\gen_array_ops.py”,第 519 行,在 _concat_v2 名字=名字)

文件“C:\Users\jhan\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py”,第 546 行,在 apply_op (input_name, op_type_name, len(values), num_attr.minimum))

ValueError:列出参数“values”到“ConcatV2”Op,长度 0 短于最小长度 2..

我的代码是:

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)

最佳答案

我不太清楚错误在哪里但是:

查看代码,错误是说 l2_losses 是空的。在此行之前放置一个 print 语句,以检查 l2_losses 的值:

print(l2_losses) # new print statement
l2_loss = tf.reduce_mean(tf.concat(axis=0, values=l2_losses), 0,
name="l2_loss")

该代码应该开箱即用,那么您对代码做了什么吗?

关于tensorflow - 值错误 : List argument 'values' to 'ConcatV2' Op with length 0 shorter than minimum length 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43355877/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com