學習使用tf2原始的梯度寇荧,計算矩陣分解卧惜;和學習保存權(quán)重和讀取權(quán)重
-
基于公式当窗,計算梯度
實現(xiàn)代碼
import tensorflow as tf
import numpy as np
import pandas as pd
import sys
from sklearn import preprocessing
learning_rate = 0.1
is_train = True
class Model(object):
def __init__(self, num_users, num_items, k):
#用戶類型數(shù)
self.num_users = num_users
#物品數(shù)
self.num_items = num_items
#特征數(shù)
self.k = k
#用戶感興趣物品類型特征embedding
self.feature_user = tf.Variable(tf.random.normal([num_users, k])) # 生成10*1的張量
#物品類型特征embedding
self.feature_item = tf.Variable(tf.random.normal([num_items, k]))
self.ckpt = self.makeCheckpoint()
def __call__(self, user_ids, item_ids):
embbeding_u = tf.nn.embedding_lookup(self.feature_user, list(np.array(user_ids)-1))
embbeding_i = tf.nn.embedding_lookup(self.feature_item, list(np.array(item_ids)-1))
#user_feature*item_feature轉(zhuǎn)置丐箩,獲取用戶與某個物品的評價(對角線上的值)
rt_ui = tf.linalg.diag_part(tf.matmul(embbeding_u, embbeding_i, transpose_b=True))
return rt_ui
def get_user_embbeding(self, user_ids):
embbeding_u = tf.nn.embedding_lookup(self.feature_user, list(np.array(user_ids)-1))
return embbeding_u
def get_items_embbeding(self, item_ids):
embbeding_i = tf.nn.embedding_lookup(self.feature_item, list(np.array(item_ids)-1))
return embbeding_i
def makeCheckpoint(self):
return tf.train.Checkpoint(
feature_user=self.feature_user,
feature_item=self.feature_item)
def saveVariables(self):
self.ckpt.save('./save/ckpt')
def restoreVariables(self):
#恢復self.feature_user和self.feature_item值
status = self.ckpt.restore(tf.train.latest_checkpoint('./save'))
status.assert_consumed() # Optional check
def loss(predicted_y, desired_y, embbeding_u, embbeding_i, ld):
plos = tf.reduce_mean(tf.square(predicted_y - desired_y))
plos_w = tf.reduce_mean(ld*tf.multiply(embbeding_u, embbeding_u)) + tf.reduce_mean(ld*tf.multiply(embbeding_i, embbeding_i))
plos = plos + plos_w
return plos
# 定義優(yōu)化器
optimizer = tf.optimizers.SGD(learning_rate)
def train(model, inputs_user_ids, inputs_item_ids, outputs, idx):
with tf.GradientTape() as t:
current_loss = loss(model(inputs_user_ids, inputs_item_ids), outputs,
model.get_user_embbeding(inputs_user_ids),
model.get_items_embbeding(inputs_item_ids), 0.001)
if idx % 100000 == 0:
print('idx:%d, loss:%f' % (idx, current_loss))
gradients = t.gradient(current_loss, [model.feature_user, model.feature_item])
optimizer.apply_gradients(zip(gradients, [model.feature_user, model.feature_item]))
df = pd.read_csv('./data/ml-1m/ratings.dat', sep='\t', names=['user', 'item', 'rating', 'timestamp'], header=None)
df = df.drop('timestamp', axis=1)
# 開始數(shù)據(jù)處理
num_items = df.item.nunique()
num_users = df.user.nunique()
print("USERS: {} ITEMS: {}".format(num_users, num_items))
r = df['rating'].values.astype(float)
min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(r.reshape(-1,1))
df_normalized = pd.DataFrame(x_scaled)
df['rating'] = df_normalized
user_ids = df['user'].values.astype(int)
item_ids = df['item'].values.astype(int)
outputs = df['rating'].values.astype(float)
print(len(user_ids), len(item_ids), len(outputs))
#最大用戶id
max_user_id = np.amax(user_ids)
#最大電影id
max_item_id = np.amax(item_ids)
model = Model(max_user_id, max_item_id, 20)
if is_train:
print(model.feature_user[:10])
max_len = len(user_ids)
batch_size = 2000
epochs = range(1000)
for epoch in epochs:
cur_idx = 0
print('Epoch %2d' % (epoch))
while True:
start_idx = cur_idx
end_idx = cur_idx + batch_size
if start_idx >= max_len:
break
if end_idx > max_len:
end_idx = max_len
cur_user_ids = user_ids[start_idx:end_idx]
cur_item_ids = item_ids[start_idx:end_idx]
cur_outputs = outputs[start_idx:end_idx]
train(model, cur_user_ids, cur_item_ids, cur_outputs, cur_idx)
cur_idx += batch_size
print(model.feature_user[0:1])
#保存權(quán)重
model.saveVariables()
else:
model.restoreVariables() #讀取權(quán)重
print('===========================================')
u1rate = tf.squeeze(tf.matmul(model.feature_user[0:1], model.feature_item, transpose_b=True))
print(u1rate);
# 獲取最大值的前10個物品
print(tf.argsort(u1rate, direction='DESCENDING')[0:10])
print('===========================================')
u1rate = tf.squeeze(tf.matmul(model.feature_user[1:2], model.feature_item, transpose_b=True))
print(u1rate);
# 獲取最大值的前10個物品
np.set_printoptions(threshold=sys.maxsize)
print(tf.argsort(u1rate, direction='DESCENDING')[0:10].numpy())
print('===========================================')