def cell(self, x): # get variables shape = [5, 5] w_hh = tf.get_variable("w_hh", shape, initializer=tf.truncated_normal_initializer(stddev=0.1)) w_xh = tf.get_variable("w_xh", shape, initializer=tf.truncated_normal_initializer(stddev=0.1)) w_hy = tf.get_variable("w_hy", shape, initializer=tf.truncated_normal_initializer(stddev=0.1)) # TODO bias # update the hidden state self.h = tf.tanh(tf.matmult(self.W_hh, self.h) + tf.matmult(self.W_xh, x)) # compute the output vector y = np.dot(self.W_hy, self.h) return y
def __init__(self, Lambda, epsilon, dim, acts, alpha=0.1): """ Lambda = discount factor alpha = learning rate dim = dimension of vector describing state epsilon = chance of not following greedy action acts = number of actions """ self.Lambda = Lambda self.alpha = alpha self.epsilon = epsilon self.dim = dim self.acts = acts # sets up the network tf.reset_default_graph() self.init = tf.initialize_all_variables() # first layer of inputs self.inputs1 = tf.placeholder(tf.float32, shape=[1, dim]) # weights for network self.weights = tf.get_variable( "weights", [dim, acts], dtype=tf.float32, initializer=tf.random_uniform_initializer) # result of one layer of computation self.Qout = tf.matmult(self.inputs1, self.weights) # index of greatest value => index of optimal action self.result = tf.argmax(self.Qout) # result of next Q values used in Bellman update equation self.nextQ = tf.placeholder(tf.float32, shape=[1, acts]) self.loss = tf.reduce_sum(tf.square(nextQ - Qout)) self.trainer = tf.train.GradientDescentOptimizer(alpha) self.updateModel = self.trainer.minimize(self.loss)
def new_fc_layer(layer_prev, num_inputs, num_outputs, use_relu=True): weights = new_weights(shape=[num_inputs, num_outputs]) bias = new_bias(length=num_outputs) layer_out = tf.add(tf.matmult(layer_prev, weights), bias) if (use_relu): layer_out = tf.nn.relu(layer_out) return layer_out
def construct_poly(poly, dR, G1, S, G2, G1S, dG1, dS, dG2, order, W=None): poly_array = tf.Variable(0., shape=[order + 1, G1.shape[0], G2.shape[0]]) poly_array[order, :, :] = dR dG1S = tf.matmult(dG1, S) G1dS = tf.matmult(G1, dS) poly_array[order - 1, :, :] = tf.matmult(dG1S, G2) + tf.matmult( G1dS, G2) + tf.matmult(G1S, dG2) if order > 1: dG1dS = tf.matmult(dG1, dS) poly_array[order - 2, :, :] = tf.matmult(dG1dS, G2) + tf.matmult( G1dS, dG2) + tf.matmult(dG1S, G2) if ordery > 2: poly_array[order - 3, :, :] = tf.matmult(dG1dS, dG2) if order > 3: raise PlatypusError('Polynomial order too large in construct_poly!') if W is not None: poly_array *= W new_order = 2 * order conv_poly_array = tf.Variable(0., shape=[new_order, G1.shape[0], G2.shape[0]]) conv_poly_array[ new_order - 1, :, :] = 2. * poly_array[order, :, :] * poly_array[order - 1, :, :] conv_poly_array[new_order - 2, :, :] = 2 * poly_array[ order, :, :] * poly_array[order - 2, :, :] + poly_array[ order - 1, :, :] * poly_array[order - 1, :, :] if order > 1: conv_poly_array[new_order - 3, :, :] = 2 * poly_array[ order, :, :] * poly_array[order - 3, :, :] + 2. * poly_array[ order - 1, :, :] * poly_array[order - 2, :, :] conv_poly_array[new_order - 4, :, :] = 2 * poly_array[ order - 3, :, :] * poly_array[order - 1, :, :] + poly_array[ order - 2, :, :] * poly_array[order - 2, :, :] if order > 2: conv_poly_array[new_order - 5, :, :] = 2 * poly_array[ order - 2, :, :] * poly_array[order - 3, :, :] conv_poly_array[new_order - 6, :, :] = poly_array[order - 3, :, :] * poly_array[order - 3, :, :] poly += tf.reduce_sum(conv_poly_array, axis=[1, 2]) return poly
def build(self): self.inputs = tf.placeholder(tf.float32, [None, frames * VARS_PER_FRAME], name="inputs") self.actions = tf.placeholder(tf.float32, [None, ACTIONS], name="actions") self.hidden_layers = [self.inputs] for i in range(layers): self.hidden_layers.append(tf.layers.dense(inputs = self.hidden_layers[-1], units = HIDDEN_UNITS, activation = tf.nn.elu, name="fc" + str(i))) self.Q_out = tf.reduce_sum(tf.matmult(self.hidden_layers[-1], self.actions)) self.Q_actual = tf.placeholder(shape=[1,ACTIONS],dtype=tf.float32) self.loss = tf.reduce_sum(tf.square(self.Q_actual - self.Q_out))
#rem to always close the sessions sess = tf.InteractiveSession() x = tf.constant(list(range(15))) print(x) sess.close() # 0-14 # Large matrix example #first find out size of memory being used import resource import numpy as np print("{} Kb".format(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss)) #create new session and define two matrices session = tf.InteractiveSession() x = tf.constant(np.eye(10000)) y = tf.constant(np.random.randn(10000, 300)) z= tf.matmult(x,y) z.eval() #print resource but be sure your computer is powerful otherwise don't run (4gb Ram +) #upnext Tensorfow in 3D from mpl_toolkits.mplot3d import Axes3D import numpy as np from matplotlib import cm import matplotlib.pyplot as plt from scipy.spatial import Delaunay def plot_basic_object(points): """Plots a basic object, assuming its convex and not too complex""" tri = Delaunay(points).convex_hull fig = plt.figure(figsize=(8, 8)) ax = fig.add_subplot(111, projection='3d')