Esempio n. 1
0
 def get_loss(self, predictions, answer):
     #predictions: [candidate_answer_num,  2* hidden_dim]
     with tf.variable_scope('projection_layer',reuse=True):
         softmax_w=tf.get_variable('softmax_w')
         softmax_b=tf.get_variable('softmax_b')
         scores=tf.matmul(predictions, softmax_w)+softmax_b
         scores=tf.squeeze(scores) #[candidate_answer_num]
         scores=tf.expand_dims(scores, 0) #[1, candidate_answer_num]
         truth=tf.onehot(answer, tf.gather(tf.shape(predictions),0))
         truth=tf.expand_dims(truth, 0)
         cross_entropy=tf.nn.softmax_cross_entropy_with_logits(logits=scores, labels=truth)
         cross_entropy=tf.squeeze(cross_entropy)
         return cross_entropy
def load_omniglot_data(data_dir, is_training):
    """
    loads training date or validation data
    """

    omniglot_data = np.load(data_dir + '/data.npy')
    omniglot_data = np.reshape(omniglot_data, newshape=(1622, 20, 28, 28, 1))
    omniglot_label = np.arange(1622)
    omniglot_label = tf.onehot(omniglot_label, 1622)
    if is_training:
        tra_data = omniglot_data[:1200]
        tra_label = omniglot_label[:1200]
        return tra_data, tra_label
    else:
        val_data = omniglot_data[1200:1411]
        val_label = omniglot_label[1200:1411]
        return val_data, val_label
Esempio n. 3
0
	def __init__(self, is_training=True):
		self.graph = Graph()
		with self.graph as_default():
			if is_training:
				self.x, self.y = get_batch_data()
			else:
				self.x = tf.placeholder(tf.int32, shape=(None, pre.maxlen))
				self.y = tf.placeholder(tf.int32, shape=(None, pre.maxlen))

			self.decoder_input = tf.concat((tf.ones_like(self.y[:,:1])*2,self.y[:,:-1]),axis=-1)
			en2id, id2en = load_vocab("./data/en_words.txt")
			ch2id, id2ch = load_vocab("./data/ch_words.txt")

			with tf.variable_scope("encoder"):
				self.enc = embedding(self.x, vocab_size=len(en2id), num_units=pre.embedding)
				key_masks = tf.expand_dims(tf.sign(tf.reduce_sum(tf.abs(self.enc),axis=-1)), -1)
				self.enc = self.enc + position_embedding(self.x, embedding_dim=pre.embedding)
				self.enc = self.enc*key_masks
				self.enc = tf.layers.dropout(self.enc, rate=0.1, training=tf.convert_to_tensor(is_training))

				for i in range(6):
					self.enc = multihead_attention(queries=self.enc,
												keys=self.enc,
												embedding_dim=pre.embedding,
												num_head=8,
												dropout_rate=0.1,
												is_training=is_training,
												future_blind=False)
					self.enc = feedforward(inputs=self.enc)

			with tf.variable_scope("decode"):
				self.dec = embedding(inputs=self.y, vocab_size=len(ch2id), embedding_dim=pre.embedding)
				key_masks = tf.expand_dims(tf.sign(tf.reduce_sum(tf.abs(self.dec), axis=-1)),-1)
				self.dec = self.dec + position_embedding(self.y, embedding_dim=pre.embedding)
				self.dec = self.dec*key_masks
				self.dec = tf.layers.dropout(self.dec, rate=0.1, training=tf.convert_to_tensor(is_training))

				for i in range(6):
					self.dec = multihead_attention(queries=self.dec,
													keys=self.dec,
													embedding_dim=pre.embedding,
													num_head=8,
													dropout_rate=0.1,
													is_training=is_training,
													future_blind=True)
					self.dec = multihead_attention(queries=self.dec,
													keys=self.enc,
													embedding_dim=pre.embedding,
													num_head=8,
													dropout_rate=0.1,
													is_training=is_training,
													future_blind=False)
					self.dec = feedforward(self.dec)

			self.logits = tf.layers.dense(self.dec, len(ch2id))
			self.preds = tf.to_int32(tf.argmax(self.logits,axis=-1))
			self.istarget = tf.to_float(tf.not_equal(self.y, 0))
			self.acc = tf.reduce_sum(tf.to_float(tf.equal(self.preds,self.y))*self.istarget)/(tf.reduce_sum(self.istarget))
			if is_training:
				self.y_smoothed = label_smoothing(tf.onehot(self.y, depth=len(ch2id)))
				self.loss = tf.nn.softmax_cross_entropy_with_logits(labels=self.y, logits=self.logits)
				self.mean_loss = tf.reduce_sum(self.loss*self.istarget)/tf.reduce_sum(self.istarget)
				self.optimizer = tf.train.AdamOptimizer(learning_rate=0.0001,
														beta1=0.9,
														beta2=0.98,
														epsilon=1e-8)
				self.opt = self.optimizer.minimize(self.mean_loss)
Esempio n. 4
0
tf.set_random_seed(777)

xy = np.loadtxt('data-04-zoo.csv', delimiter = ',',dtype=np.float32)
X_data = xy[:,0:-1]
N = X_data.shape[0]
Y_data = xy[:,[-1]]

print("y has one of the following values")

nb_classes = 7

X = tf.placeholder(tf.float32, [None, 16])
Y = tf.placeholder(tf.int32,[None, 1])

target = tf.onehot(y, nb_classes)
target = tf.reshape(target, [-1,nb_classes])
target = tf.cast(target, tf.float32)

W = tf.Variable(tf.random_normal([16, nb_classes]),name='weight')
b = tf.Variable(tf.random_normal([nb_classes]),name='bias')

def sigma(x):
    return 1./(1.+ tf.exp(-x))

def sigma_prime(x):
    return sigma(x) * (1. - sigma(x))

layer_1 = tf.matmul(X,W)+b
y_pred = sigma(layer_1)
Esempio n. 5
0
 def __init__(self, learning_rate, num_bandit, num_arm):
     #현재 환경 입력(슬롯머신 index)
     self.state_in = tf.placeholder(tf.int32, [1])
     #환경을 one_hot encoding으로 표현
     state_in_onehot = tf.onehot(self.state_in, num_bandit)
Esempio n. 6
0
    crp=resize(crp,((scale, scale))) 
    return crp

#cover.save('test-image-cover.jpeg', image.format)
#print(type(np.asarray(img)))

#def load_img(path):
# 	image = cv2.imread(path)
	
X = np.asmatrix(list2)
X = np.reshape(x,(len(list2),784))

'''
convert y to logits
'''
y_ = tf.onehot(indices=y_,depth=4)


x = tf.placeholder(shape=[None,a])
y = tf.placeholder(shape=[None,4])

w1 = tf.Variable(shape = [a,400])
b1 = tf.Variable(shape = [400,1])

w2 = tf.Variable(shape = [400,4])
b2 = tf.Variable(shape = [4,1])

 	
a1 = tf.matmul(x,w1)+b1
h1 = tf.sigmoid(a1)