Advertisement
makispaiktis

Kaggle 1 - A single neuron

Jul 11th, 2023
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.23 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. from tensorflow import keras
  3. from tensorflow.keras import layers
  4. import tensorflow as tf
  5. import matplotlib.pyplot as plt
  6.  
  7. # Set Matplotlib defaults
  8. plt.style.use('seaborn-whitegrid')
  9. plt.rc('figure', autolayout=True)
  10. plt.rc('axes', labelweight='bold', labelsize='large', titleweight='bold', titlesize=18, titlepad=10)
  11.  
  12. # 1. Read and print the dataset
  13. import pandas as pd
  14. red_wine = pd.read_csv('../input/dl-course-data/red-wine.csv')
  15. print(red_wine.head(), end='\n\n')
  16. print(red_wine.tail())
  17.  
  18. # 2. A single neuron
  19. input_shape = [11]
  20. model = keras.Sequential([ layers.Dense(units=1, input_shape=input_shape) ])
  21.  
  22. # 3. Print the weights and the bias
  23. w, b = model.weights
  24. print("Weights = \n{}\n\nBias = \n{}".format(w, b))
  25.  
  26. # 4. NOT TRAINING means: random weight and bias = 0
  27. for i in range(3):
  28.     model = keras.Sequential([ layers.Dense(1, input_shape=[1]) ])
  29.     x = tf.linspace(-1.0, 1.0, 100)
  30.     y = model.predict(x)
  31.  
  32.     plt.figure(dpi=100)
  33.     plt.plot(x, y, 'k')
  34.     plt.xlim(-1, 1)
  35.     plt.ylim(-1, 1)
  36.     plt.xlabel("Input: x")
  37.     plt.ylabel("Target y")
  38.  
  39.     w, b = model.weights        # you could also use model.get_weights() here
  40.     plt.title("Weight: {:0.2f}\nBias: {:0.2f}".format(w[0][0], b[0]))
  41.     plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement