# -*- coding: utf-8 -*-

import tensorflow as tf


class NeuralNetwork(object):

    def __init__(self, input_size, output_size, hidden_layer_sizes,
                 dtype=tf.float32):
        self._x = tf.placeholder(dtype, [None, input_size])
        self._weights = []
        self._biases = []
        self._layers = []

        prev_layer = self._x
        for layer_size in hidden_layer_sizes:
            # Add hidden layer with RELU activation.
            prev_layer = self._add_layer(prev_layer, input_size, layer_size,
                                         tf.nn.relu)
            input_size = layer_size

        # Add output layer with linear activation.
        self._y = self._add_layer(prev_layer, input_size, output_size,
                                  lambda x: x)

        # Set up trainer.
        self._y_truth = tf.placeholder(dtype, [None, output_size])
        loss = tf.reduce_mean(tf.square(self._y_truth - self._y)) / 2
        optimizer = tf.train.GradientDescentOptimizer(0.01)
        self._trainer = optimizer.minimize(loss)

        # Set up session.
        self._session = tf.Session()
        self._session.run(tf.initialize_all_variables())

        self._saver = tf.train.Saver()

    def _add_layer(self, prev_layer, input_size, output_size, activation):
        # Build layer.
        W = tf.Variable(tf.truncated_normal([input_size, output_size]))
        b = tf.Variable(tf.zeros([output_size]))
        out = activation(tf.matmul(prev_layer, W) + b)

        # Maintain references.
        self._weights.append(W)
        self._biases.append(b)
        self._layers.append(out)

        return out

    @property
    def weights(self):
        return self._session.run(self._weights)

    @property
    def biases(self):
        return self._session.run(self._biases)

    def evaluate(self, x):
        return self.evaluate_many([x])[0]

    def evaluate_many(self, xs):
        return self._session.run(self._y, feed_dict={self._x: xs})

    def train(self, x, y):
        self.train_many([x], [y])

    def train_many(self, xs, ys):
        self._session.run(self._trainer,
                          feed_dict={self._x: xs, self._y_truth: ys})

    def save(self, save_path="model.ckpt"):
        save_path = self._saver.save(self._session, save_path)
        print("Model saved in file: %s" % save_path)

    def load(self, save_path="model.ckpt"):
        self._session = tf.Session()
        self._saver.restore(self._session, save_path)
        print("Model restored.")


class Controller(object):

    def __init__(self):
        self._nn = NeuralNetwork(19, 7, [13, 10])

    def evaluate(self, x):
        return self._nn.evaluate(x)

    def train(self, x, actual):
        self._nn.train(x, actual)

    def load(self):
        self._nn.load()

    def save(self):
        self._nn.save()

@@ -18,16 +18,26 @@
 -8 -*-%0A%0A
+import os%0A
 import t
@@ -2317,25 +2317,25 @@
      print(%22
-M
+m
 odel saved i
@@ -2337,23 +2337,28 @@
 ved 
-in
+to
  file: 
-%25s%22 %25 
+%7B%7D%22.format(
 save
@@ -2363,16 +2363,17 @@
 ve_path)
+)
 %0A%0A    de
@@ -2381,28 +2381,28 @@
  load(self, 
-save
+load
 _path=%22model
@@ -2402,32 +2402,74 @@
 =%22model.ckpt%22):%0A
+        if os.path.exists(load_path):%0A    
         self._se
@@ -2489,32 +2489,36 @@
 ssion()%0A        
+  
+  
 self._saver.rest
@@ -2528,36 +2528,36 @@
 (self._session, 
-save
+load
 _path)%0A        p
@@ -2559,16 +2559,20 @@
     
+    
 print(%22
-M
+m
 odel
@@ -2584,10 +2584,36 @@
 ored
-.%22
+ from: %7B%7D%22.format(load_path)
 )%0A%0A%0A

Fix crash when no model exists