All threads / How do I temporarily redirect stderr in Ruby on Rails?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I temporarily redirect stderr in Ruby on Rails?

Alexey asked in Rails

Hi, gays. I would like to ask for advice.
I asked this question on StackOverflow , but I did not receive an answer.
Help please.

I can't get this code to work on rails! But in ruby it works.
You can try:

require 'csv'
require 'ruby-fann'

def silence_stdout(log = '/dev/null')
  orig = $stdout.dup
  $stdout.reopen(File.new(log, 'w'))
  begin
    yield
  ensure
    $stdout = orig
  end
end

x_data = []
y_data = []
# Load data from CSV file into two arrays - one for independent variables X and one for the dependent variable Y
CSV.foreach("./data/admission.csv", :headers => false) do |row|
  x_data.push( [row[0].to_f, row[1].to_f] )
  #x_data.push( [row[0].to_f, row[1].to_f] )
  y_data.push( [row[2].to_i] )
end

# Divide data into a training set and test set
test_size_percentange = 20.0 # 20.0%
test_set_size = x_data.size * (test_size_percentange/100.to_f)

test_x_data = x_data[0 .. (test_set_size-1)]
test_y_data = y_data[0 .. (test_set_size-1)]

training_x_data = x_data[test_set_size .. x_data.size]
training_y_data = y_data[test_set_size .. y_data.size]

# Setup training data model
train = RubyFann::TrainData.new( :inputs=> training_x_data, :desired_outputs=>training_y_data )

# Setup model and train using training data
model = RubyFann::Standard.new(
              num_inputs: 2,
              hidden_neurons: [6],
              num_outputs: 1 )

silence_stdout('ttt.log') do
  # 5000 max_epochs, 100 errors between reports and 0.01 desired mean-squared-error
  model.train_on_data(train, 5000, 500, 0.01)
end

# Predict single class
prediction = model.run( [45, 55] )
# Round the output to get the prediction
puts "Algorithm predicted class: #{prediction.map{ |e| e.round }}"

predicted = []
test_x_data.each do |params|
  predicted.push( model.run(params).map{ |e| e.round } )
end

correct = predicted.collect.with_index { |e,i| (e == test_y_data[i]) ? 1 : 0 }.inject{ |sum,e| sum+e }

puts "Accuracy: #{((correct.to_f / test_set_size) * 100).round(2)}% - test set of size #{test_size_percentange}%"

Tell me please how to get this output to a file? or to a string? -

Max epochs     5000. Desired error: 0.0099999998.
Epochs            1. Current error: 0.2511504292. Bit fail 80.
Epochs          500. Current error: 0.0143846525. Bit fail 3.
Epochs         1000. Current error: 0.0127008855. Bit fail 3.
Epochs         1500. Current error: 0.0125084650. Bit fail 3.
Epochs         2000. Current error: 0.0118288267. Bit fail 3.
Epochs         2500. Current error: 0.0111219604. Bit fail 2.
Epochs         3000. Current error: 0.0118827689. Bit fail 3.
Epochs         3500. Current error: 0.0118078245. Bit fail 2.
Epochs         4000. Current error: 0.0112144882. Bit fail 1.
Epochs         4500. Current error: 0.0117674908. Bit fail 2.
Epochs         5000. Current error: 0.0127671538. Bit fail 4.

I made a demo, but I can not show the console output at the finish.
Any ideas? Thank you.

P.S. admission.csv

What do you expect the output to be? A web page? If yes, you need to:

  • Put all of this code into a controller, and instead of 'puts', assign the results to an array that contains all the data you want to output.
  • Create a view that displays the content of your array.
Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 34,885+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.