Tensorflow:ValueError:形状必须等于等级,但必须为0和2 [英] Tensorflow : ValueError: Shapes must be equal rank, but are 0 and 2

查看:209
本文介绍了Tensorflow:ValueError:形状必须等于等级,但必须为0和2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

倍增(x1,Wo1)时出现形状错误.但是我找不到原因. 错误:ValueError:形状必须等于等级,但必须为0和2
将形状0与其他形状合并.输入形状为[],[20,1]的'add_2/x'(op:'Pack').

    import tensorflow as tf
    import numpy as np
    import pandas as pd
    import math

    df1=pd.read_csv(r'C:\Ocean of knowledge\Acads\7th sem\UGP\datasets\xTrain.csv')
    df1 = df1.dropna()
    xTrain = df1.values


    df2 = pd.read_csv(r'C:\Ocean of knowledge\Acads\7th sem\UGP\datasets\yTrain.csv')
    df2 = df2.dropna()
    yTrain = df2.values

    sess=tf.Session()    
    saver = tf.train.import_meta_graph(r'C:\Ocean of knowledge\Acads\7th sem\UGP\NeuralNet\my_model.meta')
    saver.restore(sess,tf.train.latest_checkpoint('./'))


    graph = tf.get_default_graph()
    w1 = graph.get_tensor_by_name("input:0")
    feed_dict ={w1:xTrain1}
    op_to_restore = graph.get_tensor_by_name("hidden:0")
    h1 = sess.run(op_to_restore,feed_dict)
    print(h1)

    n_input1 = 20
    n_hidden1 = 1

    def weight_variable(shape):
        initial = tf.truncated_normal(shape, stddev=0.1)
        return tf.Variable(initial)

    def bias_variable(shape):
        initial = tf.constant(0.1, shape=shape)
        return tf.Variable(initial)

    x1 = tf.placeholder(tf.float32, shape=[])
    Wo1 = weight_variable([20,1])
    bo1 = bias_variable([1])
    y1 = tf.nn.tanh(tf.matmul((x1,Wo1)+ bo1),name="op_to_restore2_")

    y1_ = tf.placeholder("float", [None,n_hidden1], name="check_")
    meansq1 = tf.reduce_mean(tf.square(y1- y1_), name="hello_")
    train_step1 = tf.train.AdamOptimizer(0.005).minimize(meansq1)

    #init = tf.initialize_all_variables()

    init = tf.global_variables_initializer()
    sess.run(init)

    n_rounds1 = 100
    batch_size1 = 5
    n_samp1 = 350

    for i in range(n_rounds1+1):    
        sample1 = np.random.randint(n_samp1, size=batch_size1)
        batch_xs1 = h1[sample1][:]
        batch_ys1 = yTrain[sample1][:]
        sess.run(x1, feed_dict={x1: batch_xs1, y1_:batch_ys1})

解决方案

在这里tf.matmul((x1,Wo1)+ bo1您正在使用 tf.multiply 的目的. >

I am getting shape error while multipying (x1,Wo1). But I can't find the reason for it. Error : ValueError: Shapes must be equal rank, but are 0 and 2
From merging shape 0 with other shapes. for 'add_2/x' (op: 'Pack') with input shapes: [], [20,1].

    import tensorflow as tf
    import numpy as np
    import pandas as pd
    import math

    df1=pd.read_csv(r'C:\Ocean of knowledge\Acads\7th sem\UGP\datasets\xTrain.csv')
    df1 = df1.dropna()
    xTrain = df1.values


    df2 = pd.read_csv(r'C:\Ocean of knowledge\Acads\7th sem\UGP\datasets\yTrain.csv')
    df2 = df2.dropna()
    yTrain = df2.values

    sess=tf.Session()    
    saver = tf.train.import_meta_graph(r'C:\Ocean of knowledge\Acads\7th sem\UGP\NeuralNet\my_model.meta')
    saver.restore(sess,tf.train.latest_checkpoint('./'))


    graph = tf.get_default_graph()
    w1 = graph.get_tensor_by_name("input:0")
    feed_dict ={w1:xTrain1}
    op_to_restore = graph.get_tensor_by_name("hidden:0")
    h1 = sess.run(op_to_restore,feed_dict)
    print(h1)

    n_input1 = 20
    n_hidden1 = 1

    def weight_variable(shape):
        initial = tf.truncated_normal(shape, stddev=0.1)
        return tf.Variable(initial)

    def bias_variable(shape):
        initial = tf.constant(0.1, shape=shape)
        return tf.Variable(initial)

    x1 = tf.placeholder(tf.float32, shape=[])
    Wo1 = weight_variable([20,1])
    bo1 = bias_variable([1])
    y1 = tf.nn.tanh(tf.matmul((x1,Wo1)+ bo1),name="op_to_restore2_")

    y1_ = tf.placeholder("float", [None,n_hidden1], name="check_")
    meansq1 = tf.reduce_mean(tf.square(y1- y1_), name="hello_")
    train_step1 = tf.train.AdamOptimizer(0.005).minimize(meansq1)

    #init = tf.initialize_all_variables()

    init = tf.global_variables_initializer()
    sess.run(init)

    n_rounds1 = 100
    batch_size1 = 5
    n_samp1 = 350

    for i in range(n_rounds1+1):    
        sample1 = np.random.randint(n_samp1, size=batch_size1)
        batch_xs1 = h1[sample1][:]
        batch_ys1 = yTrain[sample1][:]
        sess.run(x1, feed_dict={x1: batch_xs1, y1_:batch_ys1})

解决方案

Here tf.matmul((x1,Wo1)+ bo1 you're using tf.matmul(a,b), that's the matrix multiplication operation. This op requires that both a and b are matrices (tensor with rank >=2).

In your case, you're multiplying x1 that's defined like

x1 = tf.placeholder(tf.float32, shape=[])

and Wo1 that's defined like

Wo1 = weight_variable([20,1])

As you can see, x1 is not a matrix but is, instead, a scalar (a tensor whose shape is []).

Maybe you were looking for an element wise multiplication? That's what tf.multiply is for.

这篇关于Tensorflow:ValueError:形状必须等于等级,但必须为0和2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆