Scalar product

2

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

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

the input:

Home page -1,00
7,00
5.00

the weights:

Weights
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 the input file name and weightsneural.xlsx

  

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

     

Scale with lace for
  Climb -0.09999999999999998
  Traceback (most recent call last):
   File "C: \ Users \ g0024041 \ Documents \ Python \ Neural Networks \ perceptron1.py", line 56, in & module;      print ("Scale", INPUTS1.dot (WEIGHTS))
  ValueError: shapes (3,1) and (3,1) not aligned: 1 (dim 1)! = 3 (dim 0)

It's clear that when I use values, instead of having a shape of 3 I have the shape of 3.1 How do I convert a dataframe (since I used pd.read_excel) into an np.array? How to use the dot in this case?

    
asked by anonymous 22.09.2018 / 07:01

1 answer

0

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

just replace (add.):

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

by

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

I hope I have helped

    
25.09.2018 / 15:41