Error using a normal python code in python mode

Hi I’ve written a Code to recognize a number. It works fine for a other editor but in python mode I get the error
“Maybe there’s an unclosed paren or quote mark somewhere before this line?”

Here is my code so far:

import gzip
import numpy as np
from scipy.special import expit
from sklearn.preprocessing import OneHotEncoder
import pickle


def open_images(filename):
    with gzip.open(filename, "rb") as file:
        data = file.read()
        return np.frombuffer(data, dtype=np.uint8, offset=16)\
            .reshape(-1, 28, 28)\
            .astype(np.float32)


def open_labels(filename):
    with gzip.open(filename, "rb") as file:
        data = file.read()
        return np.frombuffer(data, dtype=np.uint8, offset=8)


X_train = open_images("train-images-idx3-ubyte.gz").reshape(-1, 784)
y_train = open_labels("train-labels-idx1-ubyte.gz")

X_test = open_images("t10k-images-idx3-ubyte.gz").reshape(-1, 784)
y_test = open_labels("t10k-labels-idx1-ubyte.gz")

class NeuralNetwork(object):
    def __init__(self):
        self.w0 = np.random.randn(100, 784)
        self.w1 = np.random.randn(10, 100)
        self.learnrate=0.1


    def activation(self, x):
        return expit(x)

    def train(self, X, y):
        a0 = self.activation(self.w0 @ X.T)
        pred = self.activation(self.w1 @ a0)

        output_error = y.T - pred
        dw1 = output_error * pred * (1 - pred) @ a0.T / len(X)

        # print(np.mean(dw1))

        a0_error = output_error.T @ self.w1
        dw0 = (a0_error.T * a0 * (1 - a0)) @ X / len(X)



        assert dw1.shape == self.w1.shape
        assert dw0.shape == self.w0.shape

        self.w1 += self.learnrate * dw1
        self.w0 += self.learnrate* dw0

    def predict(self, X):
        a0 = self.activation(self.w0 @ X.T)
        pred = self.activation(self.w1 @ a0)
        return pred

    def cost(self, pred, y):
        # SUM((y - pred)^2)
        s = (1 / 2) * (y.T - pred) ** 2
        return np.mean(np.sum(s, axis=0))

model = NeuralNetwork()
oh = OneHotEncoder()
y_train_oh = oh.fit_transform(y_train.reshape(-1, 1)).toarray()

try:
    with open('w0.p','rb') as f:  # Python 3: open(..., 'rb')
        model.w0 = pickle.load(f)
    with open('w1.p','rb') as f:  # Python 3: open(..., 'rb')
        model.w1 = pickle.load(f)
except:
    print("nofile")

def trainnet():
    for i in range(0, 1000):
        f = open('w0.p', 'wb')
        pickle.dump(model.w0, f)
        f.close()
        f = open('w1.p', 'wb')
        pickle.dump(model.w1, f)
        f.close()
        for j in range(0, 59000, 1000):

            model.train(X_train[j:(j + 1000), :] / 255., y_train_oh[j:(j + 1000), :])
        #print(j,i,str(model.cost(model.predict(X_train[j:(j + 1000), :]),y_train_oh[j:(j + 1000), :])))
        
        y_test_pred = model.predict(X_test / 255.)
        y_test_pred = np.argmax(y_test_pred, axis=0)
        print((np.mean(y_test_pred==y_test)))

Can you help me with this please?

The @ is a new operator for matrix multiplication added in Python 3.5. Processing.py uses Python 2.7.

Also, you cannot use NumPy with Processing.py:
https://github.com/jdf/processing.py/wiki

3 Likes

Ok thank you. For the answer.