训练时丢失函数的Tensorflow维度大小/等级问题

时间:2023-01-06 01:41:42

I have a custom loss function which appears to be valid (no errors when running those two lines, as below)

我有一个自定义丢失函数似乎是有效的(运行这两行时没有错误,如下所示)

alpha_cost = 2 cost = tf.reduce_mean(tf.where(tf.less(Y * out, 0), tf.squeeze((alpha_cost*out)**2 - tf.sign(Y)*out + tf.abs(Y)), tf.squeeze(tf.abs(Y - out))))

alpha_cost = 2 cost = tf.reduce_mean(tf.where(tf.less(Y * out,0),tf.squeeze((alpha_cost * out)** 2 - tf.sign(Y)* out + tf.abs( Y)),tf.squeeze(tf.abs(Y - out))))

However when I actually continue with model training (batch size 10,000), use of this loss function generates the following error ...

但是,当我实际继续进行模型训练(批量大小为10,000)时,使用此丢失函数会产生以下错误...

InvalidArgumentError (see above for traceback): Inputs to operation Select_4 of type Select must have the same size and shape. Input 0: [1,10000] != input 1: [10000] [[Node: Select_4 = Select[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](Less_7, Squeeze_6, Squeeze_7)]]

InvalidArgumentError(参见上面的回溯):操作的输入Select类型的Select_4必须具有相同的大小和形状。输入0:[1,10000]!=输入1:[10000] [[节点:Select_4 =选择[T = DT_FLOAT,_device =“/ job:localhost / replica:0 / task:0 / device:GPU:0” ](Less_7,Squeeze_6,Squeeze_7)]]

Might be a case of just needing to add another tf.squeeze to condense the dimensions... but I've tried a couple and it's not fully cooperating, so perhaps there is a bigger issue with this approach ... thanks for your help!

可能只是需要添加另一个tf.squeeze来缩小尺寸...但我已经尝试了一对它并没有完全合作,所以这种方法可能存在更大的问题...感谢您的帮助!

1 个解决方案

#1


0  

Using the numpy module,

使用numpy模块,

import numpy as np

and defining

input0 = tf.where(tf.less(Y * out, 0)
input1 = tf.squeeze((alpha_cost*out)**2 - tf.sign(Y)*out + tf.abs(Y))

Try reshaping input1 from a rank-1 array to a row-vector:

尝试将input1从rank-1数组重新整形为row-vector:

input1 = np.reshape(input1, (1, 10000)) 

To give it the same shape as input0. You can assert that input1 has the right shape:

赋予它与input0相同的形状。您可以断言input1具有正确的形状:

assert(input1.shape == (1,10000)) 

and check that input0 and input1 have the same dimensions:

并检查input0和input1是否具有相同的尺寸:

assert(input0.shape == input1.shape) 

#1


0  

Using the numpy module,

使用numpy模块,

import numpy as np

and defining

input0 = tf.where(tf.less(Y * out, 0)
input1 = tf.squeeze((alpha_cost*out)**2 - tf.sign(Y)*out + tf.abs(Y))

Try reshaping input1 from a rank-1 array to a row-vector:

尝试将input1从rank-1数组重新整形为row-vector:

input1 = np.reshape(input1, (1, 10000)) 

To give it the same shape as input0. You can assert that input1 has the right shape:

赋予它与input0相同的形状。您可以断言input1具有正确的形状:

assert(input1.shape == (1,10000)) 

and check that input0 and input1 have the same dimensions:

并检查input0和input1是否具有相同的尺寸:

assert(input0.shape == input1.shape)