简体   繁体   English

如何解决“hape必须排名2但是'MatMul_4'(op:'MatMul')排名为0的输入形状:[],[3]。”

[英]how to fix “hape must be rank 2 but is rank 0 for 'MatMul_4' (op: 'MatMul') with input shapes: [], [3].”

I'm trying to create a regression model for a dataset from .csv file but I'm getting the error 我正在尝试为.csv文件中的数据集创建回归模型,但我收到了错误

hape must be rank 2 but is rank 0 for 'MatMul_4' (op: 'MatMul') with input shapes: [], [3]. hape必须是等级2,但对于输入形状为'MatMul_4'(op:'MatMul')的等级为0:[],[3]。

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import tensorflow as tf

#importing data
dataset = pd.read_csv('Salary_Data.csv')
x_data = dataset.iloc[:,0].values
y_data = dataset.iloc[:,1].values

#split data into train and test
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test =train_test_split(x_data, y_data, test_size = 0.3, random_state = 0, shuffle = True)


#regression using TF

m = tf.Variable(0.45, dtype= tf.float64)
b = tf.Variable(0.15, dtype= tf.float64)

batchsize = 3

xph = tf.placeholder(tf.float64,[batchsize])
yph = tf.placeholder(tf.float64,[batchsize])

y_model = tf.add(tf.matmul(m, xph), b)

error = tf.reduce_mean(tf.square(yph - y_model))

optimizer = tf.train.GradientDescentOptimizer(learning_rate= 0.001)
train = optimizer.minimize(error)

init = tf.global_variables_initializer()

#session
with tf.Session() as sess:
    sess.run(init)

    batches = 7

    for i in range(batches):
        ranid = np.random.randint(len(x_train),size = batchsize)
        feed = {xph:x_train[ranid],yph:y_train[ranid]}
        sess.run(train,feed_dict = feed)

    teta1, teta0 = sess.run([m,b])



plt.scatter(x_train, y_train, color = 'red')

I tried to multiply directly using operators also but I am getting the same error 我试图直接使用运算符,但我得到相同的错误

m is just a scalar variable, so you can't do a matrix multiplication with it. m只是一个标量变量,所以你不能用它进行矩阵乘法。 You said multiplying directly doesn't work, but it seems to work fine for me: 你说直接乘法不起作用,但它似乎对我来说很好:

y_model = m*xph + b

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError: Shape must be rank 2 but is rank 1 for 'MatMul' (op: 'MatMul') with input shape: [2], [2,3] - ValueError: Shape must be rank 2 but is rank 1 for 'MatMul' (op: 'MatMul') with input shapes: [2], [2,3] 形状必须为2级,但输入形状为[100,100],[?, 15,100]的'MatMul_46'(op:'MatMul')的等级为3 - Shape must be rank 2 but is rank 3 for 'MatMul_46' (op: 'MatMul') with input shapes: [100,100], [?,15,100] ValueError:形状必须为 2 级,但“MatMul”为 1 级 - ValueError: Shape must be rank 2 but is rank 1 for 'MatMul' ValueError:Shape必须是2级,但是'MatMul'的排名是3 - ValueError: Shape must be rank 2 but is rank 3 for 'MatMul' 尺寸必须相等,但对于输入形状为[1,15],[1,500]的'MatMul_1'(op:'MatMul'),尺寸应为15和1 - Dimensions must be equal, but are 15 and 1 for 'MatMul_1' (op: 'MatMul') with input shapes: [1,15], [1,500] ValueError:尺寸必须相等,但对于'MatMul_1'(op:'MatMul'),输入形状为784和500:[?,784],[500,500] - ValueError: Dimensions must be equal, but are 784 and 500 for 'MatMul_1' (op: 'MatMul') with input shapes: [?,784], [500,500] ValueError:尺寸必须相等,但输入形状为[?,64],[4 ,?]的'MatMul'(op:'MatMul')的尺寸必须为64和4 - ValueError: Dimensions must be equal, but are 64 and 4 for 'MatMul' (op: 'MatMul') with input shapes: [?,64], [4,?] Tensorflow引发“尺寸必须相等,但输入形状为[0,100],[0,100]的'MatMul'(op:'MatMul')的尺寸必须为100和0。” - Tensorflow throws “Dimensions must be equal, but are 100 and 0 for 'MatMul' (op: 'MatMul') with input shapes: [0,100], [0,100].” ValueError:形状必须为 0 级,但对于“ReadFile”(操作:“ReadFile”)为 1 级,输入形状为:[1] - ValueError: Shape must be rank 0 but is rank 1 for 'ReadFile' (op: 'ReadFile') with input shapes: [1] Matmul有不同的级别 - Matmul with different rank
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM