Repository navigation
Expand file tree
/
Copy pathmodel.py
More file actions
102 lines (88 loc) · 4.09 KB
/
Copy pathmodel.py
File metadata and controls
102 lines (88 loc) · 4.09 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
"""
Created on Thu May 31 18:52:57 2018
@author: GY
"""
import tensorflow as tf
from tensorflow.contrib import rnn
import functools
REGULARIZATION_RATE = 0.01
def doublewrap(function):
"""
A decorator decorator, allowing to use the decorator to be used without
parentheses if not arguments are provided. All arguments must be optional.
"""
@functools.wraps(function)
def decorator(*args, **kwargs):
if len(args) == 1 and len(kwargs) == 0 and callable(args[0]):
return function(args[0])
else:
return lambda wrapee: function(wrapee, *args, **kwargs)
return decorator
@doublewrap
def define_scope(function, scope=None, *args, **kwargs):
"""
A decorator for functions that define TensorFlow operations. The wrapped
function will only be executed once. Subsequent calls to it will directly
return the result so that operations are added to the graph only once.
The operations added by the function live within a tf.variable_scope(). If
this decorator is used with arguments, they will be forwarded to the
variable scope. The scope name defaults to the name of the wrapped
function.
"""
attribute = '_cache_' + function.__name__
name = scope or function.__name__
@property
@functools.wraps(function)
def decorator(self):
if not hasattr(self, attribute):
with tf.variable_scope(name, *args, **kwargs):
setattr(self, attribute, function(self))
return getattr(self, attribute)
return decorator
class RNN_Model:
def __init__(self, inputs, labels, n_inputs=22, n_steps=24, n_hidden_units=50, n_classes=2):
self.input = inputs
self.labels = labels
self.n_inputs = n_inputs
self.n_steps = n_steps
self.n_hidden_units = n_hidden_units
self.n_classes = n_classes
self.regularizer = tf.contrib.layers.l2_regularizer(REGULARIZATION_RATE)
self.predict
self.optimizer
self.accuracy
@define_scope
def predict(self):
X = self.input
with tf.name_scope("inlayer"):
weights_in = tf.Variable(tf.random_uniform([self.n_inputs, self.n_hidden_units], -1.0, 1.0), name="in_w")
b_in = tf.Variable(tf.constant(0.1, shape=[self.n_hidden_units]), name="in_bias")
X = tf.reshape(X, [-1, self.n_inputs])
X_in = tf.matmul(X, weights_in) + b_in
X_in = tf.reshape(X_in, [-1, self.n_steps, self.n_hidden_units])
tf.add_to_collection('losses', self.regularizer(weights_in))
# RNN cell
with tf.name_scope("RNN_CELL"):
lstm_cell = rnn.BasicLSTMCell(self.n_hidden_units)
outputs, states = tf.nn.dynamic_rnn(lstm_cell, X_in, dtype=tf.float32)
# output layer
with tf.name_scope('outlayer'):
weights_out = tf.Variable(tf.random_uniform([self.n_hidden_units, self.n_classes], -1.0, 1.0), name="out_w")
b_out = tf.Variable(tf.constant(0.1, shape=[self.n_classes]), name="out_bias")
logist = tf.matmul(states[1], weights_out) + b_out
tf.add_to_collection('losses', self.regularizer(weights_out))
return logist, outputs
@define_scope
def optimizer(self, lr=0.0001):
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=self.predict[0],
labels=self.labels))+tf.add_n(tf.get_collection('losses'))
train_op = tf.train.AdamOptimizer(lr).minimize(cost)
return train_op
@define_scope
def accuracy(self):
correct_pred = tf.equal(tf.argmax(self.predict[0], 1), tf.argmax(self.labels, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=self.predict[0],
labels=self.labels))+tf.add_n(tf.get_collection('losses'))
return accuracy, cost