Skip to content

Commit

Permalink
Added example in README
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtLinkov committed Jun 14, 2018
1 parent 8ba30f6 commit caa1677
Show file tree
Hide file tree
Showing 6 changed files with 10,688 additions and 10,574 deletions.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,66 @@ puts "We managed #{correct_answers} out of #{test_data.data_pairs.size} total"
puts "Cnn output: #{cnn.output}"
```

Evolutionary optimizer example:
```crystal
label = {
"setosa" => [0.to_f64, 0.to_f64, 1.to_f64],
"versicolor" => [0.to_f64, 1.to_f64, 0.to_f64],
"virginica" => [1.to_f64, 0.to_f64, 0.to_f64],
}
iris = SHAInet::Network.new
iris.add_layer(:input, 4, :memory, SHAInet.sigmoid)
iris.add_layer(:hidden, 4, :memory, SHAInet.sigmoid)
iris.add_layer(:output, 3, :memory, SHAInet.sigmoid)
iris.fully_connect
# Get data from a local file
outputs = Array(Array(Float64)).new
inputs = Array(Array(Float64)).new
CSV.each_row(File.read(__DIR__ + "/test_data/iris.csv")) do |row|
row_arr = Array(Float64).new
row[0..-2].each do |num|
row_arr << num.to_f64
end
inputs << row_arr
outputs << label[row[-1]]
end
data = SHAInet::TrainingData.new(inputs, outputs)
data.normalize_min_max
training_data, test_data = data.split(0.9)
iris.train_es(
data: training_data,
pool_size: 50,
learning_rate: 0.5,
sigma: 0.1,
cost_function: :c_ent,
epochs: 500,
mini_batch_size: 15,
error_threshold: 0.00000001,
log_each: 100,
show_slice: true)
# Test the trained model
correct = 0
test_data.data.each do |data_point|
result = iris.run(data_point[0], stealth: true)
expected = data_point[1]
# puts "result: \t#{result.map { |x| x.round(5) }}"
# puts "expected: \t#{expected}"
error_sum = 0.0
result.size.times do |i|
error_sum += (result[i] - expected[i]).abs
end
correct += 1 if error_sum < 0.3
end
puts "Correct answers: (#{correct} / #{test_data.size})"
(correct > 10).should eq(true)
```


## Development

### Basic Features
Expand Down

0 comments on commit caa1677

Please sign in to comment.