ValueError:尺寸必须等于Tensorflow/keras

发布于 2025-02-07 17:20:03 字数 1157 浏览 4 评论 0原文

我的代码如下:

v = tf.Variable(initial_value=v, trainable=True)

(1,768)

v.shape是模型中的

inputs_sents = keras.Input(shape=(50,3))
inputs_events = keras.Input(shape=(50,768))
x_1 = tf.matmul(v,tf.transpose(inputs_events))
x_2 = tf.matmul(x_1,inputs_sents)

:但是我有一个错误,

ValueError: Dimensions must be equal, but are 768 and 50 for 
'{{node BatchMatMulV2_3}} = 
    BatchMatMulV2[T=DT_FLOAT,
    adj_x=false,
    adj_y=false](BatchMatMulV2_3/ReadVariableOp,
    Transpose_3)' with input shapes: [1,768], [768,50,?]

我认为这需要考虑批次?但是我该如何处理呢? v是可训练的向量(或第一维为1),我希望在培训过程中对其进行培训。

PS:这是我使用第一个答案提供的代码得到的结果,我认为这是不正确的,因为Keras已经考虑了第一个批次维度。

另外,从keras文档中,

形状:形状元组(整数),不包括批处理大小。例如,shape =(32,)表示预期的输入将是32维向量的批次。这个元组的元素可以是没有的; “无”元素表示形状未知的尺寸。

“ https://keras.io/api/layers/core_layers/input/”

My codes are as follow:

v = tf.Variable(initial_value=v, trainable=True)

v.shape is (1, 768)

In the model:

inputs_sents = keras.Input(shape=(50,3))
inputs_events = keras.Input(shape=(50,768))
x_1 = tf.matmul(v,tf.transpose(inputs_events))
x_2 = tf.matmul(x_1,inputs_sents)

But I got an error,

ValueError: Dimensions must be equal, but are 768 and 50 for 
'{{node BatchMatMulV2_3}} = 
    BatchMatMulV2[T=DT_FLOAT,
    adj_x=false,
    adj_y=false](BatchMatMulV2_3/ReadVariableOp,
    Transpose_3)' with input shapes: [1,768], [768,50,?]

I think it takes consideration of the batch? But how shall I deal with this?
v is a trainable vector (or 2d array with first dimension being 1), I want it to be trained in the training process.

PS: This is the result I got using the codes provided by the first answer, I think it is incorrect cause keras already takes consideration of the first batch dimension.
enter image description here

Plus, from the keras documentation,

shape: A shape tuple (integers), not including the batch size. For instance, shape=(32,) indicates that the expected input will be batches of 32-dimensional vectors. Elements of this tuple can be None; 'None' elements represent dimensions where the shape is not known.

https://keras.io/api/layers/core_layers/input/

Should I rewrite my codes without keras?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸短情长 2025-02-14 17:20:03

批处理的形状由none表示:

import numpy as np
inputs_sents = keras.Input(shape=(None,1,3))
inputs_events = keras.Input(shape=(None,1,768))
v = np.ones(shape=(1,768), dtype=np.float32)
v = tf.Variable(initial_value=v, trainable=True)
x_1 = tf.matmul(v,tf.transpose(inputs_events))
x_2 = tf.matmul(x_1,inputs_sents)

The shape of a batch is denoted by None:

import numpy as np
inputs_sents = keras.Input(shape=(None,1,3))
inputs_events = keras.Input(shape=(None,1,768))
v = np.ones(shape=(1,768), dtype=np.float32)
v = tf.Variable(initial_value=v, trainable=True)
x_1 = tf.matmul(v,tf.transpose(inputs_events))
x_2 = tf.matmul(x_1,inputs_sents)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文