Scalar product

Asked

Viewed 1,375 times

2

Objective: To make a neuron using the load of weights and inputs from an xlsx

The scalar product has been calculated in several ways as exercise, but when I try to use the dot gives error.

the input:

Entree
-1.00
7,00
5.00

the weights:

Pesos
0,80
0,10
0,00

are in a file called neural.xlsx

the code:

import xlrd
import xlwt
import sys
import csv
import os
import subprocess
import locale
import decimal
import numpy as np
from datetime import datetime, timezone , date, timedelta
import matplotlib.pyplot as plt
import unicodedata
import pandas as pd

# -*- coding: utf-8 -*-
"""
21/09/2018 

@author:Guido D'Angelo
"""
'''----------------------------------------------------------------------
Carrega pesos e inputs
----------------------------------------------------------------------
'''
Nome_arquivo=input(" entre com o nome do arquivo de inputs e pesos")
TEMP={"Mult":[]}
INPUTS=pd.read_excel(Nome_arquivo, sheet_name="INPUTS")
WEIGHTS=pd.read_excel(Nome_arquivo, sheet_name="Weight")

'''
calcula o produto escalar trabalando com array
'''
print("produto Escalar com values")
INPUTS1=INPUTS.values
WEIGHTS1=WEIGHTS.values
print(INPUTS1)
print(WEIGHTS1)
print("Escalar ",sum(INPUTS1*WEIGHTS1))

print("________________________________")



'''
Calcula o produto escalar com laço for
'''
print("Escalar com laço for")
SOMA=0.0
for i in range(0,len(INPUTS)):
               SOMA+=INPUTS.loc[i,'Entrada']*WEIGHTS.loc[i,'Pesos']

print("Escalar ",SOMA)
'''
Calcula com dot
'''
print("Escalar ",INPUTS1.dot(WEIGHTS))

as a result I have: enter input file name and neural weight.xlsx

Scalar product with values
[[-1]
[ 7]
[ 5]]
[[0.8]
[0.1]
[0. ]]
Climb [-0.1]


Climb with for loop
Scale -0.09999999999998
Traceback (Most recent call last):
File "C: Users g0024041 Documents Python Neural Networks perceptron1.py", line 56, in <module>
print("Scalar",INPUTS1.dot(WEIGHTS))
Valueerror: shapes (3,1) and (3,1) not Aligned: 1 (dim 1) != 3 (dim 0)

it is clear that when I use values, instead of having a Shape of 3 I have the Shape of 3.1 How to turn a dataframe (since I used pd.read_excel) into an np.array? How to use the dot in this case?

1 answer

0


Oops. This is a very common mistake. Actually the error is not of programming but in mathematics. When we multiply two vectors we have to transpose the second.

so just replace(add.):

print("Escalar ",INPUTS1.dot(WEIGHTS))

for

print("Escalar ",INPUTS1.dot(WEIGHTS.T))

I hope I’ve helped

Browser other questions tagged

You are not signed in. Login or sign up in order to post.