n_steps = 45 seq_width = 50 initializer = tf.random_uniform_initializer(-1,1) seq_input = tf.placeholder(tf.float32, [n_steps, batch_size, seq_width]) #sequence we will provide at runtime early_stop = tf.placeholder(tf.int32) #what timestep we want to stop at inputs = [tf.reshape(i, (batch_size, seq_width)) for i in tf.split(0, n_steps, seq_input)] #inputs for rnn needs to be a list, each item being a timestep. #we need to split our input into each timestep, and reshape it because split keeps dims by default cell = LSTMCell(size, seq_width, initializer=initializer) initial_state = cell.zero_state(batch_size, tf.float32) outputs, states = rnn.rnn(cell, inputs, initial_state=initial_state, sequence_length=early_stop) #set up lstm iop = tf.initialize_all_variables() #create initialize op, this needs to be run by the session! session = tf.Session() session.run(iop) #actually initialize, if you don't do this you get errors about uninitialized stuff feed = {early_stop:100, seq_input:np.random.rand(n_steps, batch_size, seq_width).astype('float32')} #define our feeds. #early_stop can be varied, but seq_input needs to match the shape that was defined earlier outs = session.run(outputs, feed_dict=feed) #run once
#sequence we will provide at runtime early_stop = tf.placeholder(tf.int32) #what timestep we want to stop at embedding_matrix = tf.Variable(tf.zeros([n_vocab, seq_width])) W_class = tf.Variable(tf.zeros([seq_width, n_class])) B_class = tf.Variable(tf.zeros([n_class, ])) embed_input = tf.nn.embedding_lookup(embedding_matrix, seq_input) inputs = [tf.reshape(i, (batch_size, seq_width)) for i in tf.split(0, n_steps, embed_input)] #inputs for rnn needs to be a list, each item being a timestep. #we need to split our input into each timestep, and reshape it because split keeps dims by default #cell = LSTMCell(seq_width, seq_width, initializer=initializer) cell = LSTMCell(seq_width, seq_width, initializer=initializer) initial_state = cell.zero_state(batch_size, tf.float32) outputs, states = rnn.rnn(cell, inputs, initial_state=initial_state, sequence_length=early_stop) last = outputs[-1] logit = tf.matmul(last, W_class) + B_class loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logit, y)) iop = tf.initialize_all_variables() #create initialize op, this needs to be run by the session! #session = tf.Session() session = tf.Session(config=tf.ConfigProto(log_device_placement=True)) session.run(iop) #actually initialize, if you don't do this you get errors about uninitialized stuff feed = {early_stop:n_steps,
# What timestep we want to stop at early_stop = tf.placeholder(tf.int32) initializer = tf.random_uniform_initializer(-1, 1) # Inputs for rnn needs to be a list, each item being a timestep. # we need to split our input into each timestep, and reshape it because # split keeps dims by default inputs = [ tf.reshape(i, (batch_size, input_dim)) for i in tf.split(0, n_steps, seq_input) ] with tf.device("/cpu:0"): cell1 = LSTMCell(hidden_dim, input_dim, initializer=initializer) initial_state1 = cell1.zero_state(batch_size, tf.float32) outputs1, states1 = rnn.rnn(cell1, inputs, initial_state=initial_state1, sequence_length=early_stop, scope="RNN1") with tf.device("/cpu:0"): cell2 = LSTMCell(output_dim, hidden_dim, initializer=initializer) initial_state2 = cell2.zero_state(batch_size, tf.float32) outputs2, states2 = rnn.rnn(cell2, outputs1, initial_state=initial_state2, sequence_length=early_stop, scope="RNN2")
Fx,Fy,gamma=attn_window("write",h_dec,write_n) Fyt=tf.transpose(Fy,perm=[0,2,1]) wr=tf.batch_matmul(Fyt,tf.batch_matmul(w,Fx)) wr=tf.reshape(wr,[batch_size,B*A]) #gamma=tf.tile(gamma,[1,B*A]) return wr*tf.reshape(1.0/gamma,[-1,1]) write=write_attn if FLAGS.write_attn else write_no_attn ## STATE VARIABLES ## cs=[0]*T # sequence of canvases mus,logsigmas,sigmas=[0]*T,[0]*T,[0]*T # gaussian params generated by SampleQ. We will need these for computing loss. # initial states h_dec_prev=tf.zeros((batch_size,dec_size)) enc_state=lstm_enc.zero_state(batch_size, tf.float32) dec_state=lstm_dec.zero_state(batch_size, tf.float32) ## DRAW MODEL ## # construct the unrolled computational graph for t in range(T): c_prev = tf.zeros((batch_size,img_size)) if t==0 else cs[t-1] x_hat=x-tf.sigmoid(c_prev) # error image r=read(x,x_hat,h_dec_prev) h_enc,enc_state=encode(enc_state,tf.concat(1,[r,h_dec_prev])) z,mus[t],logsigmas[t],sigmas[t]=sampleQ(h_enc) h_dec,dec_state=decode(dec_state,z) cs[t]=c_prev+write(h_dec) # store results h_dec_prev=h_dec DO_SHARE=True # from now on, share variables
wr = tf.reshape(wr, [batch_size, B * A]) #gamma=tf.tile(gamma,[1,B*A]) return wr * tf.reshape(1.0 / gamma, [-1, 1]) write = write_attn if FLAGS.write_attn else write_no_attn ## STATE VARIABLES ## cs = [0] * T # sequence of canvases mus, logsigmas, sigmas = [0] * T, [0] * T, [ 0 ] * T # gaussian params generated by SampleQ. We will need these for computing loss. # initial states h_dec_prev = tf.zeros((batch_size, dec_size)) enc_state = lstm_enc.zero_state(batch_size, tf.float32) dec_state = lstm_dec.zero_state(batch_size, tf.float32) ## DRAW MODEL ## # construct the unrolled computational graph for t in range(T): c_prev = tf.zeros((batch_size, img_size)) if t == 0 else cs[t - 1] x_hat = x - tf.sigmoid(c_prev) # error image r = read(x, x_hat, h_dec_prev) h_enc, enc_state = encode(enc_state, tf.concat(1, [r, h_dec_prev])) z, mus[t], logsigmas[t], sigmas[t] = sampleQ(h_enc) h_dec, dec_state = decode(dec_state, z) cs[t] = c_prev + write(h_dec) # store results h_dec_prev = h_dec DO_SHARE = True # from now on, share variables