application_vnd.jupyter.stderr_output_1.png
from tensorflow.keras.optimizers import RMSprop

from tensorflow.keras.preprocessing.image import ImageDataGenerator

from tensorflow.keras.preprocessing.sequence import pad_sequences

from tensorflow.keras.preprocessing.text import Tokenizer

import json

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')

def solution_model():
    url = 'https://storage.googleapis.com/download.tensorflow.org/data/sarcasm.json'
    urllib.request.urlretrieve(url, 'sarcasm.json')
    vocab_size = 1000
    embedding_dim = 16
    max_length = 120
    trunc_type = 'post'
    padding_type = 'post'
    oov_tok = '<OOV>'
    training_size = 20000
    sentences = []
    labels = []
    with open('sarcasm.json', 'r') as f:
        data = json.load(f)
        for text in data:
            sentences.append(text['headline'])
            labels.append(text['is_sarcastic'])
    train_sentences = sentences[:training_size]
    test_sentences = sentences[training_size:]
    train_labels = labels[:training_size]
    test_labels = labels[training_size:]
    tokenizer = Tokenizer(num_words=vocab_size, oov_token=oov_tok)
    tokenizer.fit_on_texts(train_sentences)
    train_sequences = tokenizer.texts_to_sequences(train_sentences)
    train_padded = pad_sequences(train_sequences, maxlen=max_length, padding=padding_type, truncating=trunc_type)
    test_sequences = tokenizer.texts_to_sequences(test_sentences)
    test_padded = pad_sequences(test_sequences, maxlen=max_length, padding=padding_type, truncating=trunc_type)
    train_labels = np.array(train_labels)
    test_labels = np.array(test_labels)
    model = tf.keras.Sequential([tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length), tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)), tf.keras.layers.Dense(24, activation='relu'), tf.keras.layers.Dense(1, activation='sigmoid')])
    model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])
    model.fit(train_padded, train_labels, epochs=10, validation_data=(test_padded, test_labels))
    return model

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