Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Another work around this "TypeError: Cannot iterate over a scalar tensor" for matplotlib?

TypeError: Cannot iterate over a scalar tensor.

Two tensor scalars are input for plt.bar() for the (x, y) values. (Converting CamDavidsonPilon Bayesian-Hackers to tensorflow2.0)

This is specifically for the "def plot_artificial_sms_dataset():" function. I tried in the code block above and it works if I cast the tensors to int32. Not sure why the solution is variable

link: https://github.com/CamDavidsonPilon/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/blob/master/Chapter2_MorePyMC/Ch2_MorePyMC_TFP.ipynb

The workaround I found is to convert both into np.array() format. i.e. np.array(x), np.array(y).

Is there another work around within tensorflow2.0? Is there another obvious solution?

plt.bar(days_range, data, color=TFColor[3])
plt.bar(tau - 1, data[tau - 1], color="r", label="user behaviour changed")
plt.xlim(0, 80);

The problem line is the one with (tau - 1). Not sure why the other one doesn't break when it is also using tensors.

My solution:

    plt.bar(days_range, data, color=TFColor[3])
    plt.bar(np.array(tau - 1), np.array(data[tau - 1]), color="r", label="user behaviour changed")
    plt.xlim(0, 80);
like image 882
user8851623 Avatar asked Oct 29 '25 20:10

user8851623


1 Answers

x.numpy(), y.numpy() converts 'x' and a 'y' to numpy arrays

like image 170
user8851623 Avatar answered Oct 31 '25 10:10

user8851623