# Error using a normal python code in python mode

**URL:** <https://discourse.processing.org/t/error-using-a-normal-python-code-in-python-mode/16389>\
**Category:** Processing.py\
**Created:** [December 14, 2019, 9:22am UTC](https://discourse.processing.org/t/error-using-a-normal-python-code-in-python-mode/16389 "2019-12-14T09:22:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [December 14, 2019, 9:22am UTC](https://discourse.processing.org/t/error-using-a-normal-python-code-in-python-mode/16389/1 "2019-12-14T09:22:20Z")

</div>

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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![tabreturn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tabreturn/32/3697_2.png) [@tabreturn](https://discourse.processing.org/u/tabreturn)\
**Post date:** [December 14, 2019, 9:27am UTC](https://discourse.processing.org/t/error-using-a-normal-python-code-in-python-mode/16389/2 "2019-12-14T09:27:38Z")

</div>

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](https://github.com/jdf/processing.py/wiki)

---

<div class="post-metadata">

**Author:** ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)\
**Post date:** [December 14, 2019, 5:06pm UTC](https://discourse.processing.org/t/error-using-a-normal-python-code-in-python-mode/16389/3 "2019-12-14T17:06:58Z")

</div>

> [@tabreturn](#):
>
> matrix

Ok thank you. For the answer.
