0

I'm going through gradient descent algorithms and I want to track how they work using some interactive tools. So I've created the code below and it works excellent. The problem is that when I try to print something from animate method I don't see the output neither Notebook nor Jupyter command line. I found this post. It looks like author is able to see print's output. It means it is possible to print it. What is wrong with my code? How can I see print results from the animate method here? BTW the print at the bottom works fine.

from numpy import random
import matplotlib.pyplot as plt
import numpy as np
import time
import matplotlib
import matplotlib.animation as animation
import time
%matplotlib notebook

X = random.normal(size=100, scale=2)
Y = 4*X + 7

# Building the model
m = 0
c = 0

L = 0.02  # The learning Rate
epochs = 1000  # The number of iterations to perform gradient descent

n = float(len(X)) # Number of elements in X

fig, ax = plt.subplots()
ax.set_xlim(min(X)-2, max(X)+2)
ax.set_ylim(min(Y)-5, max(Y)+5)
ax.scatter(X, Y)
line, = ax.plot(X, np.zeros(len(X)), 'r-')

def animate(epoch):
    print("Hello world")
    global epochs
    if(epoch >= epochs):
        return None
    global m
    global c
    global X
    global Y
    global L
    Y_pred = m*X + c
    line.set_ydata(Y_pred)
    D_m = (-2/n) * sum(X * (Y - Y_pred))
    D_c = (-2/n) * sum(Y - Y_pred)
    m = m - L * D_m
    c = c - L * D_c
    return line

ani = animation.FuncAnimation(fig=fig, func=animate, interval=100, blit=True, save_count=50)

plt.show()
   
print (m, c)
Dron4K
  • 390
  • 2
  • 4
  • 17

0 Answers0