gpt4 book ai didi

Tensorflow : ValueError: Shapes must be equal rank, 但分别是 0 和 2

转载 作者:行者123 更新时间:2023-12-03 17:37:07 24 4
gpt4 key购买 nike

乘法 (x1,Wo1) 时出现形状错误。但我找不到原因。
错误:ValueError:形状必须具有相同的等级,但为 0 和 2
将形状 0 与其他形状合并。对于具有输入形状的“add_2/x”(操作:“Pack”):[], [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})

最佳答案

这里tf.matmul((x1,Wo1)+ bo1您正在使用 tf.matmul(a,b) ,这就是矩阵乘法运算。
此操作要求 ab是矩阵(秩 >=2 的张量)。

在你的情况下,你乘以 x1这被定义为

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

Wo1这被定义为
Wo1 = weight_variable([20,1])

如您所见, x1不是矩阵,而是标量(形状为 [] 的张量)。

也许你正在寻找一个元素明智的乘法?就是这样 tf.multiply 是为了。

关于Tensorflow : ValueError: Shapes must be equal rank, 但分别是 0 和 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46273364/

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