Skip to content

Instantly share code, notes, and snippets.

@svecon
Created January 10, 2017 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svecon/0ca7e018cf2607e1669a81cf19f3df0c to your computer and use it in GitHub Desktop.
Save svecon/0ca7e018cf2607e1669a81cf19f3df0c to your computer and use it in GitHub Desktop.
import tensorflow as tf
class EvoNetwork:
def __init__(self, input_size, weights, use_biases=False):
with tf.Graph().as_default():
self.inputs = tf.placeholder(tf.float32, [None, input_size])
hidden_layer = self.inputs
for i, weight in enumerate(weights):
with tf.variable_scope('hidden_layer_{}'.format(i)):
W = tf.Variable(
initial_value=weight.astype(np.float32),
trainable=False,
name='W',
)
b = tf.Variable(
initial_value=np.zeros((weight.shape[1])).astype(np.float32),
trainable=False,
name='b',
)
hidden_layer = tf.add(tf.matmul(hidden_layer, W), b)
if i+1 < len(weights):
hidden_layer = tf.nn.tanh(hidden_layer)
self.output_layer = hidden_layer
self.session = tf.Session()
self.session.run(tf.global_variables_initializer())
def infer(self, inputs):
[infer] = self.session.run([self.output_layer], {self.inputs: inputs})
return infer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment