我想创建一个简单的电影推荐系统,Y(I,j)=第j个用户评价的第I部电影,R(I,j)=1(如果一部电影已经被评价),否则R(I,j)=0

2024-04-25 03:57:10 发布

您现在位置:Python中文网/ 问答频道 /正文

import tensorflow as tf
import pymatlab as mat
import numpy as np
import matplotlib.pyplot as plt
import scipy.io


mat=scipy.io.loadmat('ex8_movies.mat')
Y=mat['Y']
R=mat['R']
plt.interactive(False)
#plt.plot(Y)
#plt.show()

print(Y[R==1])
#print(Y[R==0])

r=tf.constant(R,dtype=tf.float32)
params=scipy.io.loadmat('ex8_movieParams.mat')
num_users = params['num_users']
num_movies =params['num_movies']
num_features=params['num_features']

print("shape of Y:",np.shape(Y))
print(num_users)
print(num_movies)
x=np.random.rand(1682,2)

X=tf.placeholder(tf.float32,([1682 ,2]))
#X=tf.Variable(tf.zeros([1682,943]),dtype=tf.float32)
thetas=tf.Variable(tf.zeros([943,2]),dtype=tf.float32)
y=tf.placeholder(tf.float32,[1682 ,943])
sess=tf.InteractiveSession()
init = tf.global_variables_initializer()
sess.run(init)
#print((np.transpose(thetas)))
j_temp=tf.square(tf.matmul(X,tf.transpose(thetas))- y)
j_temp=j_temp([R==1])
cost=tf.reduce_mean(tf.reduce_sum(j_temp))

optimizer=tf.train.GradientDescentOptimizer(0.09).minimize(cost)
sess.run(j_temp,{X:x,y:Y})
#sess.run(j_tmp)
sess.run(cost)

print("slodvhbdfh\n\n")
print(Y[R==0])

我想创建一个简单的电影推荐系统,Y(I,j)=第j个用户评价的第I部电影,R(I,j)=1(如果一部电影已经被评价),否则R(I,j)=0 我得到一个错误:

"C:\Python\python interpreter\pythonw.exe" C:/Python/Projects/recommend.py

[5 4 4 ..., 2 3 3]
shape of Y: (1682, 943)
[[943]]
[[1682]]

File "C:/Python/Projects/recommend.py", line 38, in <module>
j_temp=j_temp([R==1])
TypeError: 'Tensor' object is not callable

Process finished with exit code 1

Tags: runimporttfasnppltscipyparams
1条回答
网友
1楼 · 发布于 2024-04-25 03:57:10

identifier(arguments)是调用函数的Python语法。你试着用一个张量来做这个,这个张量不是你不能调用的函数。你想干什么?我不知道,因为您没有提供文本、注释甚至有用的变量名的描述。你知道吗

此外,构造“[R==1]”不是合法的Python。如果您试图获取该用户的所有分级电影,则需要检查语法以将筛选器应用于张量。这些关键词应该会引导你找到答案。你知道吗

相关问题 更多 >