<jupyter_start><jupyter_text>TensorFlow Basics<jupyter_code>import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

%matplotlib inline
tf.__version__<jupyter_output><empty_output><jupyter_text>Constants<jupyter_code>h = tf.constant('Hello World')
h
h.graph is tf.get_default_graph()
x = tf.constant(100)
x
# Create Session object in which we can run operations.
# A session object encapsulates the environment in which
# operations are executed. Tensor objects are evaluated
# by operations.
session = tf.Session()
session.run(h)
session.run(x)
type(session.run(x))
type(session.run(h))<jupyter_output><empty_output><jupyter_text>Operations<jupyter_code>a = tf.constant(2)
b = tf.constant(3)
with tf.Session() as session:
    print('Addition: {}'.format(session.run(a + b)))
    print('Subtraction: {}'.format(session.run(a - b)))
    print('Multiplication: {}'.format(session.run(a * b)))
    print('Division: {}'.format(session.run(a / b)))
e = np.array([[5., 5.]])
f = np.array([[2.], [2.]])
e
f
# Convert numpy arrays to TensorFlow objects
ec = tf.constant(e)
fc = tf.constant(f)
matrix_mult_op = tf.matmul(ec, fc)
with tf.Session() as session:
    print('Matrix Multiplication: {}'.format(session.run(matrix_mult_op)))<jupyter_output>Matrix Multiplication: [[ 20.]]<jupyter_text>Placeholders Instead of using a constant, we can define a placeholder that allows us to provide the value at the time of execution just like function parameters.<jupyter_code>c = tf.placeholder(tf.int32)
d = tf.placeholder(tf.int32)
add_op = tf.add(c, d)
sub_op = tf.subtract(c, d)
mult_op = tf.multiply(c, d)
div_op = tf.divide(c, d)
with tf.Session() as session:
    input_dict = {c: 11, d: 10}
    print('Addition: {}'.format(session.run(add_op, feed_dict=input_dict)))
    print('Subtraction: {}'.format(session.run(sub_op, feed_dict=input_dict)))
    print('Multiplication: {}'.format(session.run(mult_op, feed_dict=input_dict)))
    print('Division: {}'.format(session.run(div_op, feed_dict={c:11, d:11})))<jupyter_output>Addition: 21
Subtraction: 1
Multiplication: 110
Division: 1.0<jupyter_text>Variables A variable is a tensor that can change during program execution.<jupyter_code>var2 = tf.get_variable('var2', [2])
var2<jupyter_output><empty_output><jupyter_text>Classification using the MNIST dataset<jupyter_code>from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('data', one_hot=True)
type(mnist)
mnist.train.images
mnist.train.images.shape<jupyter_output><empty_output><jupyter_text>The MNIST dataset contain 55,000 images. The dimensions of each image is 28-by-28. Each vector has 784 elements because 28*28=784.<jupyter_code># Convert the vector to a 28x28 matrix
sample_img = mnist.train.images[0].reshape(28, 28)

# Show the picture
plt.imshow(sample_img, cmap='Greys')<jupyter_output><empty_output><jupyter_text>Before we begin, we specify three parameters:- the learning rate $\alpha$: how quickly should the cost function be adjusted.- training epoch: number of training cycles- batch size: batches of training data<jupyter_code>learning_rate = 0.001
training_epochs = 15
batch_size = 100<jupyter_output><empty_output><jupyter_text>Network parameters<jupyter_code># Number of classes is 10 because we have 10 digits
n_classes = 10

# Number of training examples
n_samples = mnist.train.num_examples

# The flatten array of the 28x28 image matrix contains 784 elements
n_input = 784

# Number of neurons in the hidden layers. For image data, 256 neurons
# is common because we have 256 intensity values (8-bit).
# In this example, we only use 2 hidden layers. The more hidden
# layers, we use the longer it takes for the model to run but
# more layers has the possibility of being more accurate.
n_hidden_1 = 256
n_hidden_2 = 256<jupyter_output><empty_output><jupyter_text>def multi<jupyter_code>def multilayer_perceptron(x, weights, biases):
    '''
    x: Placeholder for the data input
    weights: Dictionary of weights
    biases: Dictionary of bias values
    '''
    
    # First hidden layer with RELU activation
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
    layer_1 = tf.nn.relu(layer_1)
    
    # Second hidden layer with RELU activation
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
    layer_2 = tf.nn.relu(layer_2)
    
    # Output layer
    layer_out = tf.add(tf.matmul(layer_2, weights['out']), biases['out'])
    return layer_out
weights = {
    'h1': tf.Variable(tf.random_normal(shape=[n_input, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal(shape=[n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal(shape=[n_hidden_2, n_classes]))
}
tf.random_normal(shape=(n_input, n_hidden_1))
#tf.Session().run(weights['h1'])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12460929360283392, 'fraction_numerical': 0.025838716399249843, 'mean_word_length': 5.620689655172414, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '25774', 'license': 'MIT', 'n_tokens_mistral': 1732, 'n_tokens_neox': 1639, 'n_words': 427, 'path': 'tensor-flow-basics.ipynb', 'repo_name': 'sheikhomar/ml', 'size': '4799'}