Open mouseart opened 1 year ago
First I encountered the same issue when installing the environment, then I tried to manually configure the environment and dependencies. During the running, I encountered the same problem with you, and I searched stackoverflow found a similar problem the solution is to use an older version, so i refer to the repository file(.devcontainer/environment.yml) to replace numpy with the 1.22.3 version, and it working for me.
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 snapshots
. Specifically, the error message suggests that there's an issue with setting an array element with a sequence, resulting in an inhomogeneous shape after 2 dimensions.
To fix this issue, you need to ensure that the elements appended to snapshots
have consistent shapes. Looking at your code, it appears that you're appending a tuple containing weights
(which is a 2D array) and the average accuracy. However, the concatenation of pos_correct
and neg_correct
inside the tuple might be causing the inconsistency.
Here's my code i copied from the compiler:
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)
accuracy = (pos_correct + neg_correct) / 2.0 # Calculate average accuracy
snapshots.append((np.copy(weights), accuracy))
return np.array(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.ylabel('Accuracy') pylab.xlabel('Iteration') pylab.plot(step, snapshots[step,1], "bo") pylab.show()
def pl1(step): plotit(pos_examples, neg_examples, snapshots, step) Online Python compiler (interpreter) to run Python online.
print("Try programiz.pro"
This code ensures that the average accuracy is calculated correctly and appended to snapshots
along with weights
, resulting in consistent shapes for each element in snapshots
.
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.