python - Time-series RNN, 3-dimentional array sequence_loss -
for emotiw, make array image.
x_train array, likes(folder, image_count, 1024) #folder bundle of images of same image. y_train array, likes(folder, hidden_size) # hidden_size = 7, emotion recognition kinds -> one_hot(ex: [0,0,0,0,0,0,1]
for caculate loss, tf.contrib.layers.fully_connected or tf.contrib.seq2seq.sequence_loss used. outputs last one_hot array extracted , compared label.
but, pusing outputs last one_hot array not work.
how can do?
import tensorflow tf import pickle import os import matplotlib import numpy np import pprint pp open('./pickles/x_train_custom.p', 'rb') f: x_train = pickle.load(f) open('./pickles/y_train_custom.p', 'rb') f: y_train = pickle.load(f) x_train = np.array(x_train) y_train = np.array(y_train) batch_size = 1 max_size = len(x_train) batch_roop = max_size / batch_size image_count = 0 hidden_size = 7 seq_length = 0 x = tf.placeholder(tf.float32, [1, none, 1024]) y = tf.placeholder(tf.int32, [hidden_size]) cell = tf.contrib.rnn.basiclstmcell(num_units=hidden_size, state_is_tuple = true) outputs, _states = tf.nn.dynamic_rnn(cell, x, dtype=tf.float32) x_for_max = tf.reshape(outputs, [-1, hidden_size]) softmax_w = tf.get_variable("softmax_w", [hidden_size, 7]) softmax_b = tf.get_variable("softmax_b", [7]) outputs = tf.matmul(x_for_max, softmax_w) + softmax_b outputs = tf.reshape(outputs,[1, image_count, 7]) weights = tf.ones([7, 7]) ######################### y_pred = tf.contrib.layers.fully_connected(outputs[0, image_count-1, 0:], hidden_size, activation_fn=none) sequence_loss = tf.contrib.seq2seq.sequence_loss( logits=outputs[0, image_count-1], targets=y, weights=weights) ######################### #outputs = tf.reshape(outputs, [image_count, 7]) #loss = tf.reduce_sum(tf.square(outputs[-1] - y)) #optimizer = tf.train.adamoptimizer(0.01) #train = optimizer.minimize(loss) sess = tf.session() sess.run(tf.global_variables_initializer()) image_count = len(x_train [0]) seq_length = image_count an=sess.run([a], feed_dict={x:x_train[0].reshape(1, len(x_train[0]), 1024), y:y_train[0]}) # x:(1, image_count, 1024)
Comments
Post a Comment