fork download
  1. import tensorflow as tf
  2. import numpy as np
  3.  
  4. # Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3
  5. x_data = np.random.rand(100).astype(np.float32)
  6. y_data = x_data * 0.1 + 0.3
  7.  
  8. # Try to find values for W and b that compute y_data = W * x_data + b
  9. # (We know that W should be 0.1 and b 0.3, but TensorFlow will
  10. # figure that out for us.)
  11. W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
  12. b = tf.Variable(tf.zeros([1]))
  13. y = W * x_data + b
  14.  
  15. # Minimize the mean squared errors.
  16. loss = tf.reduce_mean(tf.square(y - y_data))
  17. optimizer = tf.train.GradientDescentOptimizer(0.5)
  18. train = optimizer.minimize(loss)
  19.  
  20. # Before starting, initialize the variables. We will 'run' this first.
  21. init = tf.initialize_all_variables()
  22.  
  23. # Launch the graph.
  24. sess = tf.Session()
  25. sess.run(init)
  26.  
  27. # Fit the line.
  28. for step in range(201):
  29. sess.run(train)
  30. if step % 20 == 0:
  31. print(step, sess.run(W), sess.run(b))
  32.  
  33. # Learns best fit is W: [0.1], b: [0.3]# your code goes here
Success #stdin #stdout #stderr 1.15s 205640KB
stdin
import random
import time
import tensorflow as tf
from tensorflow import keras

# list of possible colors
colors = ["red", "green", "violet"]

# create an empty list to store the data
data = []

# function to gather data from user
def gather_data():
    color = input("Enter a color (red, green, or violet): ")
    number = input("Enter a number (0-9): ")
    data.append((color, number))

# function to train the model on the gathered data
def train_model():
    # convert the data to a numpy array
    data_np = np.array(data)
    # split the data into input (X) and output (y)
    X = data_np[:, 0]
    y = data_np[:, 1]
    # one-hot encode the input data
    X = tf.keras.utils.to_categorical(X)
    # create a model
    model = keras.Sequential([
        keras.layers.Dense(64, input_shape=(3,), activation='relu'),
        keras.layers.Dense(32, activation='relu'),
        keras.layers.Dense(10, activation='softmax')
    ])
    model.compile(optimizer='adam', loss='categ
stdout
(0, array([0.8336869], dtype=float32), array([-0.10020426], dtype=float32))
(20, array([0.30165207], dtype=float32), array([0.19833234], dtype=float32))
(40, array([0.15451856], dtype=float32), array([0.27251318], dtype=float32))
(60, array([0.11473963], dtype=float32), array([0.29256868], dtype=float32))
(80, array([0.10398501], dtype=float32), array([0.2979909], dtype=float32))
(100, array([0.10107742], dtype=float32), array([0.2994568], dtype=float32))
(120, array([0.10029128], dtype=float32), array([0.29985315], dtype=float32))
(140, array([0.10007876], dtype=float32), array([0.29996032], dtype=float32))
(160, array([0.10002132], dtype=float32), array([0.29998925], dtype=float32))
(180, array([0.10000577], dtype=float32), array([0.2999971], dtype=float32))
(200, array([0.10000156], dtype=float32), array([0.29999924], dtype=float32))
stderr
WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow/python/util/tf_should_use.py:193: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.