-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
03 Perceptron/Perceptron. ipynb [in11] cannot run correctly #233
Comments
First I encountered the same issue when installing the environment, then I tried to manually configure the environment and dependencies. |
For anyone who don't want to change numpy version, I've correct the old code. You can replace the original code with the code below. def train_graph(positive_examples, negative_examples, num_iterations = 100):
num_dims = positive_examples.shape[1]
weights = np.zeros((num_dims,1)) # initialize weights
pos_count = positive_examples.shape[0]
neg_count = negative_examples.shape[0]
report_frequency = 15
snapshots = []
for i in range(num_iterations):
pos = random.choice(positive_examples)
neg = random.choice(negative_examples)
z = np.dot(pos, weights)
if z < 0:
weights = weights + pos.reshape(weights.shape)
z = np.dot(neg, weights)
if z >= 0:
weights = weights - neg.reshape(weights.shape)
if i % report_frequency == 0:
pos_out = np.dot(positive_examples, weights)
neg_out = np.dot(negative_examples, weights)
pos_correct = (pos_out >= 0).sum() / float(pos_count)
neg_correct = (neg_out < 0).sum() / float(neg_count)
snapshots.append((np.copy(weights),(pos_correct+neg_correct)/2.0))
return snapshots
snapshots = train_graph(pos_examples,neg_examples)
def plotit(pos_examples,neg_examples,snapshots,step):
fig = pylab.figure(figsize=(10,4))
fig.add_subplot(1, 2, 1)
plot_boundary(pos_examples, neg_examples, snapshots[step][0])
fig.add_subplot(1, 2, 2)
# pylab.plot(np.arange(len(snapshots[:,1])), snapshots[:,1])
pylab.plot(np.arange(len(snapshots)), [[acc] for arr,acc in snapshots])
pylab.ylabel('Accuracy')
pylab.xlabel('Iteration')
pylab.plot(step, snapshots[step,1], "bo")
pylab.show()
def pl1(step): plotit(pos_examples,neg_examples,snapshots,step) |
The error you're encountering seems to stem from an inconsistency in the shape of the arrays being appended to To fix this issue, you need to ensure that the elements appended to Here's my code i copied from the compiler: #-------------------------------------------------
snapshots = train_graph(pos_examples, neg_examples) def plotit(pos_examples, neg_examples, snapshots, step): def pl1(step): Write Python 3 code in this online editor and run it.print("Try programiz.pro" This code ensures that the average accuracy is calculated correctly and appended to I hope it workes/help :). |
I am trying to run
https://github.com/microsoft/AI-For-Beginners/blob/main/lessons/3-NeuralNetworks/03-Perceptron/Perceptron.ipynb of [In11] Code:
Encountered an error:
May I ask how to handle it to resolve it? Thank you very much.
The text was updated successfully, but these errors were encountered: