![](/img/trans.png)
[英]ValueError: Shape must be rank 2 but is rank 1 for 'MatMul' (op: 'MatMul') with input shapes: [2], [2,3]
[英]how to fix “hape must be rank 2 but is rank 0 for 'MatMul_4' (op: 'MatMul') with input shapes: [], [3].”
我正在嘗試為.csv文件中的數據集創建回歸模型,但我收到了錯誤
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')
我試圖直接使用運算符,但我得到相同的錯誤
m
只是一個標量變量,所以你不能用它進行矩陣乘法。 你說直接乘法不起作用,但它似乎對我來說很好:
y_model = m*xph + b
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.