When passed by class, numbers do not come out random

Asked

Viewed 50 times

1

I’m creating a neural network, and now I’m doing the part that would create the population to be trained, but I’m not getting a randomness. I just don’t understand it and I don’t know how to get around it. (except for the possibility that a function outside the class would be passed to modify the values)

the following code exemplifies the error:

import random as rd

# Criar 10 numeros aleatorios

# 10 random normal
for i in range(10):
  test = rd.randrange(-1000, 1000)
  print(test)

# 10 random por class
class NeuralNetwork:
  random = rd.randrange(-1000, 1000)

for n in range(10):
  test = NeuralNetwork()
  print(test.random)

I did it in repl.it https://repl.it/@nazesaria/Acrobaticimpurebinarysearchtree#main.py

How can I get around it otherwise?

1 answer

2


This behavior occurs because you have defined a class variable random as the value returned by rd.randrange(-1000, 1000) that this value will be the same for all instances and how only one call is made rd.randrange(-1000, 1000) when defining the class NeuralNetwork he remains the same.

A solution if its intention is to generate a different value at each reading of random would create a reading property random with the help of the developer @Property and make each reading return the value of rd.randrange(-1000, 1000):

import random as rd

class NeuralNetwork:
  
  @property
  def random(self):
      return rd.randrange(-1000, 1000)

test = NeuralNetwork()

for n in range(10):      
  print(test.random)
  • I tried to apply it to the way I built it, but unfortunately I’m not getting it, I tried it a lot. An example closer to the q structure I’m using would be like https://repl.it/@nazesaria/Lawngreenhummingusers#main.py

  • Even leaving this function to an external method I have the same problem https://repl.it/@nazesaria/Glossygigatrace#main.py

  • Not so that POO works, I made some annotations in a Fork because its code does not have permission for editing https://repl.it/@Ronaldovasques/Lawngreenhummingusers#main.py . Explain in detail what you are trying to do.

  • I made the example code to reproduce the problem, what I am doing is very simple, I made a Flappy Bird (bird game that keeps jumping), and a neural net to it, until then everything Ok! Now I want to make their evolution so that it is not a "Bird" at a time, but several, so I make a loop by creating several obj of the Bird class, to play simultaneously. Problem, when create they all receive same random values. That was an example to show that all obj in layers[0]. Weights[0] receive the same weight. But I wanted each one to come different. https://github.com/Pessutto/flappybird_ia

  • Got it. The problem is that you want when printing the value print(obj.brain.layers[0].weights[0]) it prints a different value, the language does not do this. You set a value for the variable obj.brain.layers[0].weights[0] this value only changes if you reset it. If you want each print to have a new number you have to read from a property because this one can define a function to be executed at each reading.

  • I modified https://repl.it/@Ronaldovasques/Lawngreenhummingusers#main.py to see if you agree with what you want now?

  • Vlw, I managed to get around this, but I did not get the success I wanted, I thought this was the problem of appearing only a 'Bird' on the screen, but even creating several groups with different Weights still only one appears. anyway thanks.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.