gpt4 book ai didi

python - 为什么权重不能广播到 Tensorflow 中的值?

转载 作者:太空宇宙 更新时间:2023-11-03 21:08:43 24 4
gpt4 key购买 nike

我尝试通过以下代码包含用于训练 CNN 的权重:

loss = tf.losses.mean_squared_error(label,x_op, weights = weight_mask);
global_step = tf.Variable(0,trainable=False)
learning_rate = tf.train.exponential_decay(0.0001, global_step, 5000, 0.9, staircase=False)
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
train_step = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1= 0.9, beta2=0.999, epsilon = 1e-08
,use_locking=False).minimize(loss)

sess = tf.Session(config=tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True)))
sess.run(tf.global_variables_initializer())

但是,无论出于何种原因,我遇到了以下错误代码:

ValueError Traceback (most recent call last)
<ipython-input-105-10c144d08f60> in <module>()
97
98 # Loss
---> 99 loss = tf.losses.mean_squared_error(label,x_op, weights = weight_mask);
100 global_step = tf.Variable(0,trainable=False)
101 learning_rate = tf.train.exponential_decay(0.0001, global_step, 5000, 0.9, staircase=False)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/losses/losses_impl.py in mean_squared_error(labels, predictions, weights, scope, loss_collection, reduction)
670 losses = math_ops.squared_difference(predictions, labels)
671 return compute_weighted_loss(
--> 672 losses, weights, scope, loss_collection, reduction=reduction)
673
674

/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/losses/losses_impl.py in compute_weighted_loss(losses, weights, scope, loss_collection, reduction)
204
205 with ops.control_dependencies((
--> 206 weights_broadcast_ops.assert_broadcastable(weights, losses),)):
207 losses = ops.convert_to_tensor(losses)
208 input_dtype = losses.dtype

/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/weights_broadcast_ops.py in assert_broadcastable(weights, values)
101 " values.shape=%s. weights.shape=%s." % (
102 _ASSERT_BROADCASTABLE_ERROR_PREFIX, values_rank_static,
--> 103 weights_rank_static, values.shape, weights.shape))
104 weights_shape_static = tensor_util.constant_value(weights_shape)
105 values_shape_static = tensor_util.constant_value(values_shape)

ValueError: weights can not be broadcast to values. values.rank=4. weights.rank=3. values.shape=(?, 256, 256, 3). weights.shape=(256, 256, 3).

谁能告诉我出了什么问题吗?

最佳答案

正如tensorflow文档所说,Tensor的等级要么为0,要么与标签的等级相同,并且必须可广播到标签(即所有维度必须为1,或与相应的损失维度相同) )。

并且你可以在numpy(link)中查看广播需要满足的条件,这与tensorflow相同。

General Broadcasting Rules:

When operating on two arrays, NumPy compares their shapes element-wise. It starts with the trailing dimensions, and works its way forward. Two dimensions are compatible when

1.they are equal, or

2.one of them is 1

因此您需要将weight_mask的尺寸更改为(1,256,256,3)

weight_mask = tf.expand_dims(weight_mask,axis=0)

关于python - 为什么权重不能广播到 Tensorflow 中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55210082/

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