Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

keras example

# Import the libraries required in this example:
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

inputs = keras.Input(shape=(784,), name="digits")
x = layers.Dense(64, activation="relu", name="dense_1")(inputs)
x = layers.Dense(64, activation="relu", name="dense_2")(x)
outputs = layers.Dense(10, activation="softmax", name="predictions")(x)

model = keras.Model(inputs=inputs, outputs=outputs)

(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

# Preprocess the data (NumPy arrays):
x_train = x_train.reshape(60000, 784).astype("float32") / 255
x_test = x_test.reshape(10000, 784).astype("float32") / 255

y_train = y_train.astype("float32")
y_test = y_test.astype("float32")

# Allocate 10,000 samples for validation:
x_val = x_train[-10000:]
y_val = y_train[-10000:]
x_train = x_train[:-10000]
y_train = y_train[:-10000]

model.compile(
    optimizer=keras.optimizers.RMSprop(),  # Optimizer
    # Minimize loss:
    loss=keras.losses.SparseCategoricalCrossentropy(),
    # Monitor metrics:
    metrics=[keras.metrics.SparseCategoricalAccuracy()],
)

print("Fit model on training data")
history = model.fit(
    x_train,
    y_train,
    batch_size=64,
    epochs=2,
    # Validation of loss and metrics
    # at the end of each epoch:
    validation_data=(x_val, y_val),
)

history.history

print("Evaluate model on test data")
results = model.evaluate(x_test, y_test, batch_size=128)
print("test loss, test acc:", results)

# Generate a prediction using model.predict() 
# and calculate it's shape:
print("Generate a prediction")
prediction = model.predict(x_test[:1])
print("prediction shape:", prediction.shape)
Comment

PREVIOUS NEXT
Code Example
Python :: python display name plot 
Python :: opencv python image capture 
Python :: how to add two numbers in python 
Python :: rnadom number python 
Python :: how to find 1 st digit in python 
Python :: application/x-www-form-urlencoded python 
Python :: basic tkinter gui 
Python :: how to store in parquet format using pandas 
Python :: dataframe from dict 
Python :: list python virtual environments 
Python :: list of numbers 
Python :: how to get dummies in a dataframe pandas 
Python :: Save a Dictionary to File in Python Using the dump Function of the pickle Module 
Python :: write a python program to find table of a number using while loop 
Python :: strip array of strings python 
Python :: turn df to dict 
Python :: sort series in ascending order 
Python :: create a generator from a list 
Python :: union dataframe pyspark 
Python :: how to convert to string in python 
Python :: reading json file in python 
Python :: number of words in a string python 
Python :: pandas remove outliers for multiple columns 
Python :: multiprocessing a for loop python 
Python :: remove env variable python 
Python :: python turtle get mouse position 
Python :: how to rename rengeindex pandas 
Python :: timedelta 
Python :: Double-Linked List Python 
Python :: fetch email from gmail using python site:stackoverflow.com 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =