Skip to content

Instantly share code, notes, and snippets.

View hadifar's full-sized avatar
:octocat:

Amir Hadifar hadifar

:octocat:
View GitHub Profile
[[67 46 44 65]
[67 46 44 65]
[67 35 11 0]
[67 35 11 0]
[67 35 11 0]
[67 46 44 65]
[67 46 44 65]
[67 35 11 0]]
****************************************
[[67 67]
import tensorflow as tf
tf.enable_eager_execution()
file_name = 'sentiment.csv'
batch_size = 8
num_buckets = 4
output_buffer_size = batch_size * 1000
reshuffle_each_iteration = True
random_seed = 1023
def __call__(self, inputs):
emb = self.embed(inputs)
d1 = self.dense1(emb)
d1 = self.global_pooling(d1)
outputs = self.pred(d1)
return outputs
self.global_pooling = tf.keras.layers.GlobalAveragePooling1D()
self.dense1 = tf.keras.layers.Dense(units=hid_size, activation=tf.nn.relu)
self.embed = tf.keras.layers.Embedding(vocab_size, 128)
table = tf.contrib.lookup.index_table_from_file(vocabulary_file="vocab.txt", num_oov_buckets=1)
csv_dataset = csv_dataset.map(lambda x, y: (tf.string_split([x]).values, y))
csv_dataset = csv_dataset.map(lambda x, y: (tf.cast(table.lookup(x), tf.int32), y))
csv_dataset = csv_dataset.map(lambda x, y: (x, tf.one_hot(y, 2)))
# convert string labels into numeric value
csv_dataset = csv_dataset.map(lambda x, y: (x, tf.cond(tf.equal(y, 'positive'), lambda: 1, lambda: 0)))
# change label-sentence to sentence-label
csv_dataset = csv_dataset.map(lambda x, y: (y, x))