text_plain_output_1.png
from tensorflow.keras.optimizers import RMSprop

from tensorflow.keras.preprocessing.image import ImageDataGenerator

import numpy as np

import numpy as np

import tensorflow as tf

import tensorflow as tf

import tensorflow as tf

import tensorflow as tf

import urllib

import urllib

import zipfile

def solution_model():
    xs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
    ys = np.array([5.0, 6.0, 7.0, 8.0, 9.0, 10.0], dtype=float)
    model = tf.keras.Sequential([tf.keras.layers.Dense(1, input_shape=[1])])
    model.compile(loss='mean_squared_error', optimizer='sgd')
    model.fit(xs, ys, epochs=1000)
    return model

if __name__ == '__main__':
    model = solution_model()
    model.save('mymodel.h5')

def solution_model():
    fashion_mnist = tf.keras.datasets.fashion_mnist
    (training_images, training_labels), (val_images, val_label) = fashion_mnist.load_data()
    training_images = training_images / 255.0
    val_images = val_images / 255.0
    training_images = np.expand_dims(training_images, axis=3)
    val_images = np.expand_dims(val_images, axis=3)
    model = tf.keras.Sequential([tf.keras.layers.Conv2D(14, (3, 3), activation='relu', input_shape=(28, 28, 1)), 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=['acc'])
    model.fit(training_images, training_labels, validation_data=(val_images, val_label), epochs=5)
    return model

if __name__ == '__main__':
    model = solution_model()
    model.save('mymodel.h5')

def solution_model():
    _TRAIN_URL = 'https://storage.googleapis.com/download.tensorflow.org/data/horse-or-human.zip'
    _TEST_URL = 'https://storage.googleapis.com/download.tensorflow.org/data/validation-horse-or-human.zip'
    urllib.request.urlretrieve(_TRAIN_URL, 'horse-or-human.zip')
    local_zip = 'horse-or-human.zip'
    zip_ref = zipfile.ZipFile(local_zip, 'r')
    zip_ref.extractall('tmp/horse-or-human/')
    zip_ref.close()
    urllib.request.urlretrieve(_TEST_URL, 'testdata.zip')
    local_zip = 'testdata.zip'
    zip_ref = zipfile.ZipFile(local_zip, 'r')
    zip_ref.extractall('tmp/testdata/')
    zip_ref.close()
    training_data = 'tmp/horse-or-human/'
    val_data = 'tmp/testdata/'
    train_datagen = ImageDataGenerator(rescale=1.0 / 255)
    validation_datagen = ImageDataGenerator(rescale=1.0 / 255.0)
    train_generator = train_datagen.flow_from_directory(training_data, target_size=(300, 300), batch_size=128, class_mode='binary')
    validation_generator = validation_datagen.flow_from_directory(val_data, target_size=(300, 300), batch_size=64, class_mode='binary')
    model = tf.keras.models.Sequential([tf.keras.layers.Conv2D(16, (3, 3), activation='relu', input_shape=(300, 300, 3)), tf.keras.layers.MaxPooling2D(2, 2), tf.keras.layers.Conv2D(32, (3, 3), activation='relu'), tf.keras.layers.MaxPooling2D(2, 2), tf.keras.layers.Conv2D(64, (3, 3), activation='relu'), tf.keras.layers.MaxPooling2D(2, 2), tf.keras.layers.Conv2D(64, (3, 3), activation='relu'), 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(512, activation='relu'), tf.keras.layers.Dense(1, activation='sigmoid')])
    model.compile(loss='binary_crossentropy', optimizer=RMSprop(lr=0.001), metrics=['accuracy'])
    model.fit(train_generator, epochs=10, verbose=1, validation_data=validation_generator)
    return model

if __name__ == '__main__':
    model = solution_model()
    model.save('mymodel2.h5')