IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed почему?

Пишу нейронную сеть для определения гендера по росту и весу, но выходит ошибка:

Traceback (most recent call last):
  File "/home/dgdays/neural_network/learner.py", line 60, in <module>
    E = sparse_cross_entropy(z, y)
  File "/home/dgdays/neural_network/learner.py", line 39, in sparse_cross_entropy
    return -np.log(z[0, y])
IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

Подскажите, пожалуйста, в чём ошибка?

Код:

import numpy as np
from numpy import random
from numpy.core.fromnumeric import argmax
from numpy.core.numeric import outer
import random
from pandas import read_csv
import matplotlib.pyplot as plt

# Входные значения

data = read_csv('/home/dgdays/neural_network/data.csv')

gender, height, weight = data['Gender'], data['Height'], data['Weight']

loss_arr = []

INPUT_DIM = 2
OUT_DIM = 2
H1_DIM = 5
H2_DIM = 10

# Веса и смещение
w1 = np.random.randn(INPUT_DIM, H1_DIM)
b1 = np.random.randn(H1_DIM)
w2 = np.random.randn(H1_DIM, H2_DIM)
b2 = np.random.randn(H2_DIM)
w3 = np.random.randn(H2_DIM, OUT_DIM)
b3 = np.random.randn(OUT_DIM)

ALPHA = 0.001
NUM_EPOCHS = 100

def relu(t):
    return np.maximum(t, 0)

def softmax(t):
    out = np.exp(t)
    return out / np.sum(out)

def sparse_cross_entropy(z, y):
    return -np.log(z[0, y])

def to_full(y, num_classes):
    y_full = np.zeros((1, num_classes))
    y_full[0, y] = 1
    return y_full

def relu_deriv(t):
    return (t >= 0).astupe(float)

for ep in range(NUM_EPOCHS):
    for i in range(len(gender)):

        x, y = (height[i], weight[i]), gender[i]
        # Forward
        t1 = x @ w1 + b1
        h1 = relu(t1)
        t2 = h1 @ w2 + b2
        h2 = relu(t2)
        t3 = h2 @ w3 + b3
        z = softmax(t3)
        E = sparse_cross_entropy(z, y)

        # Backward
        y_full = to_full(y, OUT_DIM)
        dE_dt3 = z * y_full
        dE_dw3 = h2.T @ dE_dt3
        dE_db3 = dE_dt3
        dE_dt2 = h2 * y_full
        dE_dw2 = h1.T @ dE_dt2
        dE_db2 = dE_dt2
        dE_dh1 = dE_dt2 @ w2.T
        dE_dt1 = dE_dh1 * relu_deriv(t1)
        dE_dw1 = x.T @ dE_dt1
        dE_db1 = dE_dt1

        # Update
        w1 = w1 - ALPHA * dE_dw1
        b1 = b1 - ALPHA * dE_db1
        w2 = w2 - ALPHA * dE_dw2
        b2 = b2 - ALPHA * dE_db2
        w3 = w3 - ALPHA * dE_dw3
        b3 = b3 - ALPHA * dE_db3

        loss_arr.append(E)

def predict(x):
    t1 = x @ w1 + b1
    h1 = relu(t1)
    t2 = h1 @ w2 + b2
    h2 = relu(t2)
    t3 = h2 @ w3 + b3
    z = softmax(t3)
    return z

def calc_accuracy():
    correct = 0
    for i in range(len(gender)):
        x, y = (height[i], weight[i]), gender[i]
        z = predict(x)
        y_pred = np.argmax(z)
        if y_pred == y:
            correct += 1

    acc = correct / len(gender)
    return acc

accuracy = calc_accuracy()
print('Accuracy: ', accuracy)

plt.plot(loss_arr)
plt.show()

Ссылка на датасет


Ответы (0 шт):