The default beta_initializer for layers.batch_normalization is: tf.zeros_initializer().
Is it possible to create a new initializer with an arbitrary value?
See the list of built-in initializers. The one that interests you is tf.constant_initializer:
Initializer that generates tensors with constant values.
Related
I am serializing two nearly-identical fields on a single model, and when I call the accessor method for one of them, I get the deserialized object, but when I call the accessor method for the other one, I get the ActiveRecord::AttributeMethods::Serialization::Attribute struct.
The difference I see is that for the first field, I declare serialize in an included block of a mixin Module; for the second field, I declare serialize in the class declaration (as normal).
What's causing this? How can I bring the behavior of these two fields into uniformity?
Rails 3.2.13
I found the solution. I moved the problematic serialize declaration (the one which did not produce an automatically-deserializing accessor method) upward a couple of lines so that it appeared above my attr_encrypted declarations.
I was looking through the docs for instance_variable_set and saw that the sample code given does this:
obj.instance_variable_set(:#instnc_var, "value for the instance variable")
which then allows you to access the variable as #instnc_var in any of the class's instance methods.
I'm wondering why there needs to be a colon : before the #instnc_var. What does the colon do?
My first instinct is to tell you not to use instance_variable_set unless you really know what you are using it for. It's essentially a tool for metaprogramming or a hack to bypass visibility of the instance variable.
That is, if there is no setter for that variable, you can use instance_variable_set to set it anyway. It's far better if you control the code to just create a setter.
Try looking at accessors for the vast majority of your instance variable setting needs. They are an idiomatic way to have getters and setters for your instance variables "for free" without you having to write those functions.
If you really do need instance_variable_set, it allows the first argument to be a symbol which is the name of the instance variable to set. A colon is part of the Ruby language that is like a "symbol literal": when you type :foo, you've created a symbol with value foo, just like when you type "bar" directly into your code, you create a string literal with value "bar".
I've read that ruby objects are just places where we can store instance variables (and class pointers). So:
class Person
def initialize(age)
#age = age
end
end
Now if we run:
p = Person.new(101)
Then we get:
#<Person:0x86cf5b8 #age=101>
Great, the property age is stored as an instance variable, as expected. But things work a little differently if we convert the model to inherit from ActiveRecord. Now, after instantiating an new Person, we see this:
# timestamps removed
#<Person id: 1, age: 101 >
The age property no longer appears to be an instance variable. So what is really going on here?
I know that we can access the #attributes instance variable, which contains a hash of all the properties and values, so I'm wondering if ActiveRecord is possibly modifying the console output to present the objects attributes in this way.
Is it possible to instantiate a Ruby object where properties are held as attributes and not instance variables, without using ActiveRecord?
Yes, you can extend a ruby class with include ActiveModel::AttributeMethods to expose your instance variables as ActiveModel-like attributes.
See the docs for more information.
as you see in your code 'storing' properties as instance vars was your own doing, so if you wanna hold them any other way, is also up to you. ruby gives you convenience class methods to define getter and setter methods like attr_accessor.
also worth noting, that if you inherit from ActiveRecord::Base, you should not override initialize.
I'm wondering if ActiveRecord is possibly modifying the console output to present the objects attributes in this way.
Well, kind of. The method responsible for that is inspect, and it's implemented by Object in a way that (emplasis mine):
...shows the object's class name, an encoding of the object id, and a list of the instance variables and their values (by calling inspect on each of them).
There's more right after:
User defined classes should override this method to provide a better representation of obj.
This is exactly what ActiveRecord does and the reason why you're seeing this output. The overridden method does not list instance variables, but displays AR attributes.
So just because you aren't seeing the instance variable in the console doesn't mean it isn't there!
I'm just learning rails and have noticed that when I create an object that inherits from ActiveRecord::Base (i.e. from a model I migrated), the instance variables in the object do not have a # symbol in front of them.
Is this a rails thing, or did I misunderstand something while learning ruby?
Thanks in advance for your help.
Rails doesn't use individual instance variables to store field data. Instead it makes certain methods available to you which set the correct variables. It helps Rails better populate models when using finds and allows other methods that improve how dynamic Rails is.
When accessing the "instance variables" of your object, you're actually interacting with the getter/setter methods defined by rails which in turn interact with the real instance variables.
This is actually very useful as it allows you to override them when required to modify the behaviour of the variables within your classes.
The columns of your model are not stricto sensu instance variables.
You have access to their getter/setter but they are by nature different: they are meant to be persisted.
Rails defines getter/setter for all model attributes.
Getter/setter can ben declared with attr_accessor function.
class Foo
attr_accessor :bar
def do_something
self.bar=2
#bar=2 # does the same as above
end
end
I'm trying to do some XNA development with IronRuby but are struggling with both generics (Load) and accessing some of the base-class properties such as Content.
Any hints?
Regarding Generics - if you want to create a generic object, use square brackets in order to define the generic type. For example:
list = System::Collections::Generic::List[System::String].new
Regarding base class properties, there is no "base" keyword in Ruby so you can use "self" or just call the method or property directly. You might also try to mangle the property name (for instance, HelloWorld is mangled to hello_world). I suggest that in order to access the Content propery, just call it this way:
self.content
Hope it helps,
Shay.