Skip to content

Instantly share code, notes, and snippets.

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# some reshaping here...
X = tf.placeholder(tf.float32, [None, 784], name="X")
Y_truth = tf.placeholder(tf.float32, [None, 10], name="Y")
# define model, loss, and optimizer
@hadifar
hadifar / tf.data 1.py
Last active September 15, 2018 15:07
dataset = tf.data.TextLineDataset("file.txt")
for line in dataset:
print(line)
@hadifar
hadifar / tf.data 2.py
Last active September 15, 2018 15:07
> TypeError: 'TextLineDataset' object is not iterable
or
> RuntimeError: dataset.__iter__() is only supported when eager execution is enabled.
@hadifar
hadifar / tf.data 3.py
Last active September 15, 2018 15:07
print(type(dataset))
>>> <class 'tensorflow.python.data.ops.readers.TextLineDataset'>
@hadifar
hadifar / tf.data 4.py
Last active September 15, 2018 15:06
import tensorflow as tf
dataset = tf.data.TextLineDataset("file.txt")
iterator = dataset.make_one_shot_iterator()
next_element = iterator.get_next()
with tf.Session() as sess:
print(sess.run(next_element))
print(sess.run(next_element))
print(sess.run(next_element))
import tensorflow as tf
dataset = tf.data.TextLineDataset("file.txt")
iterator = dataset.make_initializable_iterator()
next_element = iterator.get_next()
init_op = iterator.initializer
with tf.Session() as sess:
# Initialize the iterator
sess.run(init_op)
@hadifar
hadifar / tf.data 5.py
Last active September 15, 2018 15:06
This is an example how to work with tf.data module in Tensorflow.
import time
import numpy as np
import tensorflow as tf
# Step 1: Read in data
(train_x, train_y), (test_x, test_y) = tf.keras.datasets.mnist.load_data()
m_train = train_x.shape[0]
m_test = test_x.shape[0]
@hadifar
hadifar / tf.data 6.py
Last active September 15, 2018 15:06
import time
import numpy as np
import tensorflow as tf
# Step 1: Read in data
(train_x, train_y), (test_x, test_y) = tf.keras.datasets.mnist.load_data()
m_train = train_x.shape[0]
m_test = test_x.shape[0]
@hadifar
hadifar / tf.data 7.py
Last active September 15, 2018 15:06
# create training Dataset and batch it
train_data = tf.data.Dataset.from_tensor_slices(train)
train_data = train_data.shuffle(10000) # if you want to shuffle your data
train_data = train_data.batch(128)
# create testing Dataset and batch it
test_data = tf.data.Dataset.from_tensor_slices(test)
test_data = test_data.shuffle(10000)
test_data = test_data.batch(128)
@hadifar
hadifar / tf.data 8.py
Last active September 15, 2018 15:06
# create weights and bias for logistic regression
# w is initialized to random variables with mean of 0, stddev of 0.01
# b is initialized to 0
# shape of w depends on the dimension of X and Y so that Y = tf.matmul(X, w)
# shape of b depends on Y
w = tf.get_variable(name='weight',
initializer=tf.truncated_normal(shape=[784, 10], mean=0, stddev=0.01))
b = tf.get_variable(name='bias', initializer=tf.zeros([10]))