<jupyter_start><jupyter_text><jupyter_code>import tensorflow as tf
import numpy as np
from tensorflow import keras<jupyter_output><empty_output><jupyter_text>Single Layer Single Neuron<jupyter_code>model = tf.keras.Sequential([keras.layers.Dense(units=1,input_shape=[1])])
model.compile(loss='mean_squared_error', optimizer="sgd")
xs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0],dtype=float)
ys = np.array([-2.0, 1.0, 4.0, 7.0, 10.0, 13.0],dtype=float)
model.fit(xs,ys,epochs=500)
print(model.predict([10]))<jupyter_output>[[31.00325]]<jupyter_text>Fashion MNIST classification<jupyter_code>mnist = tf.keras.datasets.fashion_mnist
(training_images, training_labels), (testing_images, testing_labels) = mnist.load_data()
import matplotlib.pyplot as plt
plt.imshow(training_images[0])
print(training_labels[0])
print(training_images[0])
training_images[0].shape
training_images = training_images/255.0
testing_images = testing_images/255.0
model = tf.keras.models.Sequential([tf.keras.layers.Flatten(),tf.keras.layers.Dense(128,activation=tf.nn.relu), tf.keras.layers.Dense(10, activation=tf.nn.softmax)])
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy",metrics=['accuracy'])
model.fit(training_images,training_labels, epochs= 5)
model.evaluate(testing_images,testing_labels)
classifications = model.predict(testing_images)
print(classifications[0])
print(testing_labels[0])
plt.imshow(testing_images[0])<jupyter_output>[0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
9<jupyter_text>Using Convolutions<jupyter_code>import cv2
from scipy import misc
i = misc.ascent()
plt.grid(False)
plt.gray()
plt.axis('off')
plt.imshow(i)
plt.show()
i_transformed = np.copy(i)
size_x = i_transformed.shape[0]
size_y = i_transformed.shape[1]
print(str(size_x) + " " + str(size_y))
filter = [ [-1, -2, -1], [0, 0, 0], [1, 2, 1]] #vertical
#filter =  [[-1,0,1],[-2,0,2],[-1,0,1]] #Horizontal
weight = 1
for x in range(1,size_x-1):
  for y in range(1,size_y-1):
      output_pixel = 0.0
      output_pixel = output_pixel + (i[x - 1, y-1] * filter[0][0])
      output_pixel = output_pixel + (i[x, y-1] * filter[0][1])
      output_pixel = output_pixel + (i[x + 1, y-1] * filter[0][2])
      output_pixel = output_pixel + (i[x-1, y] * filter[1][0])
      output_pixel = output_pixel + (i[x, y] * filter[1][1])
      output_pixel = output_pixel + (i[x+1, y] * filter[1][2])
      output_pixel = output_pixel + (i[x-1, y+1] * filter[2][0])
      output_pixel = output_pixel + (i[x, y+1] * filter[2][1])
      output_pixel = output_pixel + (i[x+1, y+1] * filter[2][2])
      output_pixel = output_pixel * weight
      if(output_pixel<0):
        output_pixel=0
      if(output_pixel>255):
        output_pixel=255
      i_transformed[x, y] = output_pixel
plt.gray()
plt.grid(False)
plt.imshow(i_transformed)
#plt.axis('off')
plt.show()
new_x = int(size_x/2)
new_y = int(size_y/2)
newImage = np.zeros((new_x, new_y))
for x in range(0, size_x, 2):
  for y in range(0, size_y, 2):
    pixels = []
    pixels.append(i_transformed[x, y])
    pixels.append(i_transformed[x+1, y])
    pixels.append(i_transformed[x, y+1])
    pixels.append(i_transformed[x+1, y+1])
    pixels.sort(reverse=True)
    newImage[int(x/2),int(y/2)] = pixels[0]
 
# Plot the image. Note the size of the axes -- now 256 pixels instead of 512
plt.gray()
plt.grid(False)
plt.imshow(newImage)
#plt.axis('off')
plt.show()
mnist = tf.keras.datasets.fashion_mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()
training_images=training_images.reshape(60000, 28, 28, 1)
training_images=training_images / 255.0
test_images = test_images.reshape(10000, 28, 28, 1)
test_images=test_images / 255.0
model = tf.keras.models.Sequential([
  tf.keras.layers.Conv2D(64, (3, 3), activation='relu', input_shape=(28, 28, 1)),
  tf.keras.layers.MaxPooling2D(2, 2),
  tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
  tf.keras.layers.MaxPooling2D(2,2),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.summary()
model.fit(training_images, training_labels, epochs=5)
test_loss, test_accuracy = model.evaluate(test_images, test_labels)
print ('Test loss: {}, Test accuracy: {}'.format(test_loss, test_accuracy*100))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1675603217158177, 'fraction_numerical': 0.04803395889186774, 'mean_word_length': 4.95345744680851, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '443568', 'license': 'MIT', 'n_tokens_mistral': 1912, 'n_tokens_neox': 1788, 'n_words': 298, 'path': 'Tensorflow_1.ipynb', 'repo_name': 'ColdCoffee21/Foundations-of-Data-Science', 'size': '4476'}