[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

adding array of numbers

Chris Bond

10/3/2007 3:39:00 PM

Given a file that looks like so:
7 ==> training Set length
11 ==> file length
1 0 0 0 0 1 0 0
0 1 1 1 0 0 1 0
1 0 1 0 1 0 1 1
1 0 1 1 1 1 1 1
0 0 1 0 1 1 1 1
1 1 0 1 0 1 1 0
0 0 0 1 0 1 0 1
1 1 0 0 1 0 0 1
1 1 0 0 0 0 0 0
0 1 0 1 0 1 1 0
1 1 1 1 1 0 0 0 ==>end of file
#training set #Answer
For now, I need to do this : get the dotProduct of the vector training
set and weight vector, and if that number is > 0 , output 1 ,else output
0. later in the problem I compare that to the answer, but I'm not there
yet. Here is what I have(with tests) to try to get 1 training set to add
up all numbers(without the dotProduct of weights) using IO.readlines,
splitting each line using split, for loops to get an array of integers
to be added together to get output for Perceptron_Bond object named
einstein. coding is as follows
class Perceptron_Bond

attr_reader :weights, :output
attr_writer :weights, :output
def initialize(input, weights)
@weights = weights
@input =input
end


def output
y = @input.inject(0) {|sum, element | sum+element}
@output=y
end
end
str = ARGV[0]
inputs = Array.new
inputs1 = Array.new
inputsNum = Array.new
setArr = Array.new
answers = Array.new
arr = IO.readlines(str)


arr.shift
arr.shift

arr.each { |y| inputs1.push(y.split)}
inputs1.each do|x|
answers.push(x.last)
x.pop
end
for i in 0...inputs1.length
setArr[i]= Array.new
for j in 0...inputs1[i].length
inputsNum[j] = Array.new
inputsNum[j].push(Integer(inputs1[i][j]))
if j==(inputs1[i].length-1) then
inputs[i] = inputsNum
end
end
puts inputs[i].to_s

end
puts inputs.to_s
puts inputs[2][1].class ==> Array #i want fixnum

initWeights = Array.new(inputs.length) { |index| index =0 }
einstein = Perceptron_Bond.new(inputs[0] , initWeights)
puts einstein.output #should produce fixnum 2
i get an error as well in output telling me I cannot coerce an Array to
a fixnum
Im guessing its stemming from the nested for loops I have, but I am just
stumped
I need help in a bad way, please.

Attachments:
http://www.ruby-...attachment/524/s...

--
Posted via http://www.ruby-....

5 Answers

Axel Etzold

10/3/2007 4:14:00 PM

0


-------- Original-Nachricht --------
> Datum: Thu, 4 Oct 2007 00:39:09 +0900
> Von: Chris Bond <grnmoonanite@gmail.com>
> An: ruby-talk@ruby-lang.org
> Betreff: adding array of numbers

> Given a file that looks like so:
> 7 ==> training Set length
> 11 ==> file length
> 1 0 0 0 0 1 0 0
> 0 1 1 1 0 0 1 0
> 1 0 1 0 1 0 1 1
> 1 0 1 1 1 1 1 1
> 0 0 1 0 1 1 1 1
> 1 1 0 1 0 1 1 0
> 0 0 0 1 0 1 0 1
> 1 1 0 0 1 0 0 1
> 1 1 0 0 0 0 0 0
> 0 1 0 1 0 1 1 0
> 1 1 1 1 1 0 0 0 ==>end of file
> #training set #Answer
> For now, I need to do this : get the dotProduct of the vector training
> set and weight vector, and if that number is > 0 , output 1 ,else output
> 0. later in the problem I compare that to the answer, but I'm not there
> yet. Here is what I have(with tests) to try to get 1 training set to add
> up all numbers(without the dotProduct of weights) using IO.readlines,
> splitting each line using split, for loops to get an array of integers
> to be added together to get output for Perceptron_Bond object named
> einstein. coding is as follows
> class Perceptron_Bond
>
> attr_reader :weights, :output
> attr_writer :weights, :output
> def initialize(input, weights)
> @weights = weights
> @input =input
> end
>
>
> def output
> y = @input.inject(0) {|sum, element | sum+element}
> @output=y
> end
> end
> str = ARGV[0]
> inputs = Array.new
> inputs1 = Array.new
> inputsNum = Array.new
> setArr = Array.new
> answers = Array.new
> arr = IO.readlines(str)
>
>
> arr.shift
> arr.shift
>
> arr.each { |y| inputs1.push(y.split)}
> inputs1.each do|x|
> answers.push(x.last)
> x.pop
> end
> for i in 0...inputs1.length
> setArr[i]= Array.new
> for j in 0...inputs1[i].length
> inputsNum[j] = Array.new
> inputsNum[j].push(Integer(inputs1[i][j]))
> if j==(inputs1[i].length-1) then
> inputs[i] = inputsNum
> end
> end
> puts inputs[i].to_s
>
> end
> puts inputs.to_s
> puts inputs[2][1].class ==> Array #i want fixnum
>
> initWeights = Array.new(inputs.length) { |index| index =0 }
> einstein = Perceptron_Bond.new(inputs[0] , initWeights)
> puts einstein.output #should produce fixnum 2
> i get an error as well in output telling me I cannot coerce an Array to
> a fixnum
> Im guessing its stemming from the nested for loops I have, but I am just
> stumped
> I need help in a bad way, please.
>
> Attachments:
> http://www.ruby-...attachment/524/s...
>
> --
> Posted via http://www.ruby-....

Dear Chris,

if I understand you correctly, a first problem is that you need
to get the scalar product of two vectors.

I'd like to suggest that you use GSL

http://www.gnu.org/sof...

and its Ruby bindings ruby-gsl

http://rb-gsl.ruby...

to do these manipulations - they have them implemented in C code,
so the actual computations are faster than in Ruby, besides the
fact that you don't have to recode everything by yourself.

I have written some code below to extract a matrix and the answer
vector from a file, and to calculate the dot product.

I think you can go on from there ... or just ask again :)


Best regards,

Axel


---------------------------------------------


require "gsl"

=begin

I assume that the file "dat.txt" contains

1 0 0 0 0 1 0 0
0 1 1 1 0 0 1 0
1 0 1 0 1 0 1 1
1 0 1 1 1 1 1 1
0 0 1 0 1 1 1 1
1 1 0 1 0 1 1 0
0 0 0 1 0 1 0 1
1 1 0 0 1 0 0 1
1 1 0 0 0 0 0 0
0 1 0 1 0 1 1 0
1 1 1 1 1 0 0 0

=end

text=IO.readlines("dat.txt")
matrix_lines=[]
answer_vec=[]
text.each{|entry|
s=entry.split(/ +/)
matrix_lines<<s[0...-1].collect{|y| eval(y).to_f}
answer_vec<<eval(s[-1]).to_f
}


matrix=Matrix.alloc(matrix_lines.flatten,11,7)
answer_vec=Vector.alloc(answer_vec)
matrix_vector=Vector.alloc(matrix.column(0).to_a)

p matrix_vector.to_a
p answer_vec.to_a

# dot-product of the first column vector of the Matrix with answer
p matrix_vector*(answer_vec.col)


--
GMX FreeMail: 1 GB Postfach, 5 E-Mail-Adressen, 10 Free SMS.
Alle Infos und kostenlose Anmeldung: http://www.gmx.net/de/g...

Robert Klemme

10/4/2007 1:12:00 PM

0

2007/10/3, Chris Bond <grnmoonanite@gmail.com>:
> Given a file that looks like so:
> 7 ==> training Set length
> 11 ==> file length
> 1 0 0 0 0 1 0 0
> 0 1 1 1 0 0 1 0
> 1 0 1 0 1 0 1 1
> 1 0 1 1 1 1 1 1
> 0 0 1 0 1 1 1 1
> 1 1 0 1 0 1 1 0
> 0 0 0 1 0 1 0 1
> 1 1 0 0 1 0 0 1
> 1 1 0 0 0 0 0 0
> 0 1 0 1 0 1 1 0
> 1 1 1 1 1 0 0 0 ==>end of file
> #training set #Answer
> For now, I need to do this : get the dotProduct of the vector training
> set and weight vector, and if that number is > 0 , output 1 ,else output
> 0. later in the problem I compare that to the answer, but I'm not there
> yet. Here is what I have(with tests) to try to get 1 training set to add
> up all numbers(without the dotProduct of weights) using IO.readlines,
> splitting each line using split, for loops to get an array of integers
> to be added together to get output for Perceptron_Bond object named
> einstein. coding is as follows
> class Perceptron_Bond
>
> attr_reader :weights, :output
> attr_writer :weights, :output
> def initialize(input, weights)
> @weights = weights
> @input =input
> end
>
>
> def output
> y = @input.inject(0) {|sum, element | sum+element}
> @output=y
> end
> end
> str = ARGV[0]
> inputs = Array.new
> inputs1 = Array.new
> inputsNum = Array.new
> setArr = Array.new
> answers = Array.new
> arr = IO.readlines(str)
>
>
> arr.shift
> arr.shift
>
> arr.each { |y| inputs1.push(y.split)}
> inputs1.each do|x|
> answers.push(x.last)
> x.pop
> end
> for i in 0...inputs1.length
> setArr[i]= Array.new
> for j in 0...inputs1[i].length
> inputsNum[j] = Array.new
> inputsNum[j].push(Integer(inputs1[i][j]))
> if j==(inputs1[i].length-1) then
> inputs[i] = inputsNum
> end
> end
> puts inputs[i].to_s
>
> end
> puts inputs.to_s
> puts inputs[2][1].class ==> Array #i want fixnum
>
> initWeights = Array.new(inputs.length) { |index| index =0 }
> einstein = Perceptron_Bond.new(inputs[0] , initWeights)
> puts einstein.output #should produce fixnum 2
> i get an error as well in output telling me I cannot coerce an Array to
> a fixnum
> Im guessing its stemming from the nested for loops I have, but I am just
> stumped
> I need help in a bad way, please.
>
> Attachments:
> http://www.ruby-forum.com/attachment/524/s...

I am not sure whether I understood properly which of the vectors you
want to multiply with which ones. Is this what you want?

Kind regards

robert

Chris Bond

10/4/2007 5:58:00 PM

0

Robert Klemme wrote:
> 2007/10/3, Chris Bond <grnmoonanite@gmail.com>:
>> 1 1 0 0 1 0 0 1
>> to be added together to get output for Perceptron_Bond object named
>>
>> answers = Array.new
>> end
>>
>> stumped
>> I need help in a bad way, please.
>>
>> Attachments:
>> http://www.ruby-...attachment/524/s...
>
> I am not sure whether I understood properly which of the vectors you
> want to multiply with which ones. Is this what you want?
>
> Kind regards
>
> robert

The problem was to make a perceptron learning algorithm. you take the 1
1 0 0 1 0 0 string then initially multiply it by the weight vector
of all 0s. The output is the dot product of the two vectors. Then you
compare that output to the answers that are on the right. If the
perceptron did not fire(0), when it should have, you add the dot product
to the old weight vector, and the converse(1 when it should be 0) you
subtract
here is the speudocode
w= random vector
repeat
Get input x
Get neuron output y = (1 if w â?? x â?¥ 0; otherwise 0)
Find correct output y#
Compute error e= y# â?? y
If e â?  0
w= w + ex
until w stops changing

hope that helps
--
Posted via http://www.ruby-....

ara.t.howard

10/4/2007 8:46:00 PM

0


On Oct 3, 2007, at 9:39 AM, Chris Bond wrote:

> Given a file that looks like so:
> 7 ==> training Set length
> 11 ==> file length
> 1 0 0 0 0 1 0 0
> 0 1 1 1 0 0 1 0
> 1 0 1 0 1 0 1 1
> 1 0 1 1 1 1 1 1
> 0 0 1 0 1 1 1 1
> 1 1 0 1 0 1 1 0
> 0 0 0 1 0 1 0 1
> 1 1 0 0 1 0 0 1
> 1 1 0 0 0 0 0 0
> 0 1 0 1 0 1 1 0
> 1 1 1 1 1 0 0 0 ==>end of file
> #training set #Answer


not answering your question, but you *really* want to be using NArray

http://narray.ruby...

kind regards.

a @ http://draw...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Chris Bond

10/12/2007 7:30:00 PM

0

ok, so I got the thing to work, with just arrays, but now i want to ask
the user if he/she wants to go through the set again, with 'yes' and
'no' as the values, and what I end up with is an infinite loop.

while gets != 'no'
einstein.input.each_index { |x| einstein.c_weights(einstein.input[x],
einstein.weights, matrix_ans[x][0]) }
end

I was wandering if having the ARGV[0] as the filename has anything to do
with it.

Attachments:
http://www.ruby-...attachment/640/s...

--
Posted via http://www.ruby-....