-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoRec.py
More file actions
81 lines (56 loc) · 2.88 KB
/
Copy pathAutoRec.py
File metadata and controls
81 lines (56 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# -*- coding: utf-8 -*-
"""
@author: Parit Kansal
"""
import numpy as np
import tensorflow as tf
class AutoRec(object):
def __init__(self, visibleDimensions, epochs=200, hiddenDimensions=50, learningRate=0.1, batchSize=100):
self.visibleDimensions = visibleDimensions
self.epochs = epochs
self.hiddenDimensions = hiddenDimensions
self.learningRate = learningRate
self.batchSize = batchSize
self.optimizer = tf.keras.optimizers.RMSprop(self.learningRate)
def Train(self, X):
self.initialize_weights_biases()
for epoch in range(self.epochs):
for i in range(0, X.shape[0], self.batchSize):
epochX = X[i:i+self.batchSize]
self.run_optimization(epochX)
print("Trained epoch ", epoch)
def GetRecommendations(self, inputUser):
# Feed through a single user and return predictions from the output layer.
rec = self.neural_net(inputUser)
# It is being used as the return type is Eager Tensor.
return rec[0]
def initialize_weights_biases(self):
# Create varaibles for weights for the encoding (visible->hidden) and decoding (hidden->output) stages, randomly initialized
self.weights = {
'h1': tf.Variable(tf.random.normal([self.visibleDimensions, self.hiddenDimensions])),
'out': tf.Variable(tf.random.normal([self.hiddenDimensions, self.visibleDimensions]))
}
# Create biases
self.biases = {
'b1': tf.Variable(tf.random.normal([self.hiddenDimensions])),
'out': tf.Variable(tf.random.normal([self.visibleDimensions]))
}
def neural_net(self, inputUser):
#tf.set_random_seed(0)
# Initialization of weights and biases was moved out to the initialize_weights_biases function above
# This lets us avoid resetting them on every batch of training, which was a bug in earlier versions of
# this script.
# Create the input layer
self.inputLayer = inputUser
# hidden layer
hidden = tf.nn.sigmoid(tf.add(tf.matmul(self.inputLayer, self.weights['h1']), self.biases['b1']))
# output layer for our predictions.
self.outputLayer = tf.nn.sigmoid(tf.add(tf.matmul(hidden, self.weights['out']), self.biases['out']))
return self.outputLayer
def run_optimization(self, inputUser):
with tf.GradientTape() as g:
pred = self.neural_net(inputUser)
loss = tf.keras.losses.MSE(inputUser, pred)
trainable_variables = list(self.weights.values()) + list(self.biases.values())
gradients = g.gradient(loss, trainable_variables)
self.optimizer.apply_gradients(zip(gradients, trainable_variables))