为了账号安全,请及时绑定邮箱和手机立即绑定

您必须为占位符张量“Placeholder”提供一个值,其数据类型为浮点型和形状

您必须为占位符张量“Placeholder”提供一个值,其数据类型为浮点型和形状

江户川乱折腾 2022-01-05 19:24:22
我在我的 Pycharm 中编写了以下代码,它在 Tensorflow 中执行完全连接层 (FCL)。占位符发生无效参数错误。所以我在占位符中输入了所有dtype, shape, 和name,但我仍然收到无效参数错误。我想通过 FCL 模型制作新的 Signal(1, 222)。输入信号(1, 222) => 输出信号(1, 222)maxPredict: 找出输出信号中具有最高值的索引。calculate Y: 获取maxPredict对应的频率数组值。loss:使用真实 Y 之间的差异并将 Y 计算为损失。loss = tf.abs(trueY - calculateY)`代码(发生错误)x = tf.placeholder(dtype=tf.float32, shape=[1, 222], name='inputX')错误InvalidArgumentError(回溯见上文):您必须使用 dtype float 和 shape [1,222] tensorflow.python.framework.errors_impl.InvalidArgumentError 为占位符张量“inputX”提供一个值:您必须为占位符张量“inputX”提供一个值与 dtype float 和 shape [1,222] [[{{node inputX}} = Placeholderdtype=DT_FLOAT, shape=[1,222], _device="/job:localhost/replica:0/task:0/device:CPU:0"]] 在处理上述异常,又发生了一个异常:新的错误案例我改变了我的代码。x = tf.placeholder(tf.float32, [None, 222], name='inputX')错误案例 1tensorFreq = tf.convert_to_tensor(basicFreq, tf.float32)newY = tf.gather(tensorFreq, maxPredict) * 60loss = tf.abs(y - tf.Variable(newY))ValueError:initial_value 必须指定一个形状:Tensor("mul:0", shape=(?,), dtype=float32)错误案例 2tensorFreq = tf.convert_to_tensor(basicFreq, tf.float32)newY = tf.gather(tensorFreq, maxPredict) * 60loss = tf.abs(y - newY)回溯(最近一次调用):文件“D:/PycharmProject/DetectionSignal/TEST_FCL_StackOverflow.py”,第 127 行,trainStep = opt.minimize(loss) 文件“C:\Users\Heewony\Anaconda3\envs\TSFW_pycharm\lib \site-packages\tensorflow\python\training\optimizer.py”,第 407 行,最小化([str(v) for _,v in grads_and_vars],loss))ValueError:没有为任何变量提供梯度,检查你的图表对于不支持梯度的操作,变量之间 [tf.Variable 'Variable:0' shape=(222, 1024) dtype=float32_ref, tf.Variable 'Variable_1:0' shape=(1024,) dtype=float32_re, .. ....... tf.Variable 'Variable_5:0' shape=(222,) dtype=float32_ref] 和损失 Tensor("Abs:0", dtype=float32)。开发环境操作系统平台和发行版:Windows 10 x64TensorFlow 安装自:AnacondaTensorflow 1.12.0 版:蟒蛇 3.6.7:移动设备:不适用重现的确切命令:N/AGPU 型号和内存:NVIDIA GeForce CTX 1080 TiCUDA/cuDNN:9.0/7.4
查看完整描述

2 回答

?
森林海

TA贡献2011条经验 获得超2个赞

应该有两件事要改变。


错误案例 0。您不需要重塑层之间的流程。您可以None在第一个维度上使用来传递动态批量大小。


错误案例 1.您可以直接使用 newY 作为 NN 的输出。您只使用 tf.Variable 来定义权重或偏差。


错误案例 2.似乎 tensorflow 没有梯度下降实现,也tf.abs()没有tf.gather()。对于回归问题,均方误差通常就足够了。


在这里,我如何重写您的代码。我没有你的 matlab 部分,所以我无法调试你的 python/matlab 接口:


模型:


def Model_FCL(inputX):

    # Fully Connected Layer 1

    fcW1 = tf.get_variable('w1', shape=[222, 1024], initializer=tf.initializer.truncated_normal())

    fcb1 = tf.get_variable('b1', shape=[222], initializer=tf.initializer.truncated_normal())

    # fcb1 = tf.get_variable('b1', shape=[None, 222], trainable=False, initializer=tf.constant_initializer(valueThatYouWant)) # if you want to fix your bias constant

    fch1 = tf.nn.relu(tf.matmul(inputX, fcW1) + fcb1, name='relu1')


    # Fully Connected Layer 2

    fcW2 = tf.get_variable('w2', shape=[1024, 1024], initializer=tf.initializer.truncated_normal())

    fcb2 = tf.get_variable('b2', shape=[222], initializer=tf.initializer.truncated_normal())

    # fcb2 = tf.get_variable('b2', shape=[None, 222], trainable=False, initializer=tf.constant_initializer(valueThatYouWant)) # if you want to fix your bias constant

    fch2 = tf.nn.relu(tf.matmul(fch1, fcW2) + fcb2, name='relu2')


    # Output Layer

    fcW3 = tf.get_variable('w3', shape=[1024, 222], initializer=tf.initializer.truncated_normal())

    fcb3 = tf.get_variable('b3', shape=[222], initializer=tf.initializer.truncated_normal())

    # fcb2 = tf.get_variable('b2', shape=[None, 222], trainable=False, initializer=tf.constant_initializer(valueThatYouWant)) # if you want to fix your bias constant

    logits = tf.add(tf.matmul(fch2, fcW3), fcb3)


    predictY = tf.nn.softmax(logits)  #I'm not sure that it will learn if you do softmax then abs/MSE

    return predictY, logits

图形:


with myGraph.as_default():

    # define input data & output data 입력받기 위한 placeholder

    # put None(dynamic batch size) not -1 at the first dimension so that you can change your batch size

    x = tf.placeholder(tf.float32, shape=[None, 222], name='inputX')  # Signal size = [1, 222]

    y = tf.placeholder(tf.float32, shape=[None], name='trueY')  # Float value size = [1]


    ...


    predictY, logits = Model_FCL(x)  # Predict Signal, size = [1, 222]

    maxPredict = tf.argmax(predictY, 1, name='maxPredict')  # Find max index of Predict Signal


    tensorFreq = tf.convert_to_tensor(basicFreq, tf.float32)

    newY = tf.gather(tensorFreq, maxPredict) * 60   # Find the value that corresponds to the Freq array index


    loss = tf.losses.mean_squared_error(labels=y, predictions=newY)  # maybe use MSE for regression problem

    # loss = tf.abs(y - newY)  # Calculate absolute (true Y - predict Y) #tensorflow doesn't have gradient descent implementation for tf.abs

    opt = tf.train.AdamOptimizer(learning_rate=0.0001)

    trainStep = opt.minimize(loss)


查看完整回答
反对 回复 2022-01-05
?
小怪兽爱吃肉

TA贡献1852条经验 获得超1个赞

变量batchSignal的类型或形状似乎错误。它必须是一个 numpy 形状的数组[1, 222]。如果要使用一批大小为n × 222的示例,占位符x的形状应为[None, 222]和占位符y形状 [None]

顺便说一下,考虑使用tf.layers.dense而不是显式初始化变量并自己实现层。


查看完整回答
反对 回复 2022-01-05
  • 2 回答
  • 0 关注
  • 269 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号