Created
April 22, 2018 08:45
-
-
Save Swalloow/c96df6c597888cc37ae574fc86635564 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Model: | |
def __init__(self, data, target): | |
self.data = data | |
self.target = target | |
self._prediction = None | |
self._optimize = None | |
self._error = None | |
@property | |
def prediction(self): | |
if not self._prediction: | |
data_size = int(self.data.get_shape()[1]) | |
target_size = int(self.target.get_shape()[1]) | |
weight = tf.Variable(tf.truncated_normal([data_size, target_size])) | |
bias = tf.Variable(tf.constant(0.1, shape=[target_size])) | |
incoming = tf.matmul(self.data, weight) + bias | |
self._prediction = tf.nn.softmax(incoming) | |
return self._prediction | |
@property | |
def optimize(self): | |
if not self._optimize: | |
cross_entropy = -tf.reduce_sum(self.target, tf.log(self.prediction)) | |
optimizer = tf.train.RMSPropOptimizer(0.03) | |
self._optimize = optimizer.minimize(cross_entropy) | |
return self._optimize | |
@property | |
def error(self): | |
if not self._error: | |
mistakes = tf.not_equal( | |
tf.argmax(self.target, 1), tf.argmax(self.prediction, 1)) | |
self._error = tf.reduce_mean(tf.cast(mistakes, tf.float32)) | |
return self._error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment