Class: Newral::Data::Base
- Inherits:
-
Object
- Object
- Newral::Data::Base
- Defined in:
- lib/newral/data/base.rb
Instance Attribute Summary collapse
-
#inputs ⇒ Object
Returns the value of attribute inputs.
-
#labels ⇒ Object
Returns the value of attribute labels.
-
#outputs ⇒ Object
Returns the value of attribute outputs.
Instance Method Summary collapse
- #add_input(input, output: nil, label: nil) ⇒ Object
- #count_outputs ⇒ Object
- #downsample_input!(height: 1, width: 1, width_of_line: nil) ⇒ Object
-
#initialize(inputs: [], outputs: [], labels: []) ⇒ Base
constructor
A new instance of Base.
- #inputs_for_output(output) ⇒ Object
- #label_for_input(input) ⇒ Object
- #normalized_inputs(normalized_high: 1, normalized_low: -1 )) ⇒ Object
-
#output_as_vector(category: :all) ⇒ Object
this will make it easier to use outputs for neura networks as it translates them to vectors like [1,0,0] if you have 3 possible outputs this will return [1,0,0],,[0,0,1].
- #output_for_input(input) ⇒ Object
- #output_hash(normalized_high: 1, normalized_low: -1 )) ⇒ Object
- #output_normalized ⇒ Object
- #sample(offset: 0, limit: 100) ⇒ Object
- #sub_set(set: :inputs, category: :all) ⇒ Object
- #values_for(searched_value, only_first: false, return_objects: [], search_objects: []) ⇒ Object
Constructor Details
#initialize(inputs: [], outputs: [], labels: []) ⇒ Base
Returns a new instance of Base.
14 15 16 17 18 19 |
# File 'lib/newral/data/base.rb', line 14 def initialize( inputs: [], outputs: [], labels:[] ) @inputs = inputs @outputs = outputs @labels = labels raise Errors::InputOutputSizeMismatch unless @inputs.size == @outputs.size end |
Instance Attribute Details
#inputs ⇒ Object
Returns the value of attribute inputs.
13 14 15 |
# File 'lib/newral/data/base.rb', line 13 def inputs @inputs end |
#labels ⇒ Object
Returns the value of attribute labels.
13 14 15 |
# File 'lib/newral/data/base.rb', line 13 def labels @labels end |
#outputs ⇒ Object
Returns the value of attribute outputs.
13 14 15 |
# File 'lib/newral/data/base.rb', line 13 def outputs @outputs end |
Instance Method Details
#add_input(input, output: nil, label: nil) ⇒ Object
21 22 23 24 25 |
# File 'lib/newral/data/base.rb', line 21 def add_input( input, output: nil, label:nil ) @labels << label @outputs << output @inputs << input end |
#count_outputs ⇒ Object
117 118 119 120 121 122 123 124 |
# File 'lib/newral/data/base.rb', line 117 def count_outputs output_hash = {} @outputs.each do |output| output_key = output.size == 1 ? output.first.to_s.to_sym : output.join('-') output_hash[output_key] = (output_hash[output_key] || 0) + 1 end output_hash end |
#downsample_input!(height: 1, width: 1, width_of_line: nil) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/newral/data/base.rb', line 130 def downsample_input!( height:1, width: 1, width_of_line: nil ) raise DownSamplingImpossible unless @inputs.first.size % ( width*height ) == 0 total_height = @inputs.first.size/width_of_line @inputs.collect! do |input| downsampled = [] pos = 0 while pos < input.size do matrix = [] height.times do |h| start_pos = pos+(width_of_line*h) end_pos = pos+width+(width_of_line)*h-1 matrix = matrix+input[start_pos..end_pos] end downsampled << ( matrix.inject(0){|sum,e| sum+e }/matrix.length.to_f > 0.5 ? 1 : 0 ) pos = pos+width pos=pos+width_of_line*(height-1) if (pos%width_of_line) == 0 end downsampled end end |
#inputs_for_output(output) ⇒ Object
65 66 67 |
# File 'lib/newral/data/base.rb', line 65 def inputs_for_output( output ) values_for output, search_objects: @outputs, return_objects: @inputs end |
#label_for_input(input) ⇒ Object
61 62 63 |
# File 'lib/newral/data/base.rb', line 61 def label_for_input( input ) values_for input, search_objects: @inputs, return_objects: @labels, only_first: true end |
#normalized_inputs(normalized_high: 1, normalized_low: -1 )) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/newral/data/base.rb', line 69 def normalized_inputs(normalized_high: 1, normalized_low:-1 ) return [] if @inputs.size == 0 || !@inputs.first.kind_of?( Array ) max_values = [Float::MIN]*@inputs.first.size min_values = [Float::MAX]*@inputs.first.size @inputs.each do |input| input.each_with_index do |value,idx| max_values[idx] = value.to_f if value > max_values[idx] min_values[idx] = value.to_f if value < min_values[idx] end end @inputs.collect do |input| row = [0]*input.size input.each_with_index do |value,idx| row[idx] = (value-min_values[idx])/(max_values[idx]-min_values[idx]).to_f*(normalized_high-normalized_low)+normalized_low end row end end |
#output_as_vector(category: :all) ⇒ Object
this will make it easier to use outputs for neura networks as it translates them to vectors like [1,0,0] if you have 3 possible outputs this will return [1,0,0],,[0,0,1]
108 109 110 111 112 113 114 115 |
# File 'lib/newral/data/base.rb', line 108 def output_as_vector( category: :all ) hash = output_hash sub_set( set: :outputs, category: category ).collect do |k| vector = [0]*output_hash.keys.size vector[ output_hash.keys.index( k ) ] = 1 # output_hash.keys vector end end |
#output_for_input(input) ⇒ Object
57 58 59 |
# File 'lib/newral/data/base.rb', line 57 def output_for_input( input ) values_for input, search_objects: @inputs, return_objects: @outputs, only_first: true end |
#output_hash(normalized_high: 1, normalized_low: -1 )) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/newral/data/base.rb', line 88 def output_hash( normalized_high: 1, normalized_low:-1 ) @output_hash = @outputs.inject({}) do |hash,output| hash[output] = ( hash[output] || 0 )+1 hash end new_hash = {} @output_hash.keys.sort.each_with_index do |key,idx| new_hash[ key ] = normalized_low+((normalized_high.to_f-normalized_low)*idx)/( [@output_hash.keys.length-1,1].max ) end @output_hash = new_hash end |
#output_normalized ⇒ Object
100 101 102 103 |
# File 'lib/newral/data/base.rb', line 100 def output_normalized hash = output_hash @outputs.collect{ |k| hash[k]} end |
#sample(offset: 0, limit: 100) ⇒ Object
126 127 128 |
# File 'lib/newral/data/base.rb', line 126 def sample( offset:0,limit:100 ) Base.new( inputs: @inputs[offset..limit+offset] , outputs: @outputs[offset..limit+offset] ) end |
#sub_set(set: :inputs, category: :all) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/newral/data/base.rb', line 27 def sub_set( set: :inputs, category: :all ) data = case set when :inputs then @inputs when :outputs then @outputs else raise Errors::UnknownSet end case category when :all then data when :training then data[0..(data.size.to_f*0.7).to_i] when :validation then data[(data.size.to_f*0.7).to_i+1..(data.size.to_f*0.8).to_i ] when :testing then data[(data.size.to_f*0.8).to_i+1,data.size ] else raise Errors::UnknownCategory, category.to_s end end |
#values_for(searched_value, only_first: false, return_objects: [], search_objects: []) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/newral/data/base.rb', line 45 def values_for( searched_value, only_first: false, return_objects: [], search_objects: [] ) results = [] search_objects.each_with_index do |each_value,idx| if only_first return return_objects[idx] if searched_value == each_value || [searched_value] == each_value else results << return_objects[idx] if searched_value == each_value || [searched_value] == each_value end end results unless only_first end |