Skip to content

Instantly share code, notes, and snippets.

@idcesares
Created May 29, 2021 15:06
Show Gist options
  • Select an option

  • Save idcesares/80718fbd5ef6305040442f14f66e8525 to your computer and use it in GitHub Desktop.

Select an option

Save idcesares/80718fbd5ef6305040442f14f66e8525 to your computer and use it in GitHub Desktop.
Simple Neural Network using Tensorflow
# Imports
import tensorflow as tf
import numpy as np
from tensorflow import keras
# Define and compile the Neural Network (NN)
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])]) # Only 1 neuron
# Compile the NN
model.compile(optimizer='sgd', loss='mean_squared_error')
# Simple test data
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)
# Train the NN
model.fit(xs, ys, epochs=50)
# Predict result for new number
print(model.predict([10.0]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment