Convolution layer in keras - keras

I am exploring convolution layer in keras from:
https://github.com/fchollet/keras/blob/master/keras/layers/convolutional.py#L233
everywhere i found following type of code lines:
#interfaces.legacy_conv1d_support
#interfaces.legacy_conv2d_support
what is working and role of these lines. I searched in google but not find answer anywhere. please explain.

These lines starting with # are called decorators in python. Check out this page to read a brief summary about them. The basic function of this decorators is, that they wrap the following function into another function which has some kind of "wrapper" functions, like preprocessing the arguments, changing the accessibility of the function etc.
Taking a look at the interfaces.py file you will see this:
legacy_conv1d_support = generate_legacy_interface(
allowed_positional_args=['filters', 'kernel_size'],
conversions=[('nb_filter', 'filters'),
('filter_length', 'kernel_size'),
('subsample_length', 'strides'),
('border_mode', 'padding'),
('init', 'kernel_initializer'),
('W_regularizer', 'kernel_regularizer'),
('b_regularizer', 'bias_regularizer'),
('W_constraint', 'kernel_constraint'),
('b_constraint', 'bias_constraint'),
('bias', 'use_bias')],
preprocessor=conv1d_args_preprocessor)
So, the use of this function is basicly to rename parameters. Why is this? The keras API changed the names of some arguments of some functions (like W_regularizer -> kernel_regularizer). To allow users to be able to run old code, they added this decorator, which will just replace the names of old arguments with the corresponding new parameter name before calling the real function. This allows you to run "old" keras 1 code, even though you have installed keras 2.
Tl;dr: These lines are just used to for compatibility reasons. As this are just internal aspects of keras there is nothing you have to worry about or to take care of.

Related

Python SCons Action?

I've been searching the documentation for a long time and still can't figure this out.
In the SCons documentation, there is discussion about adding a PreAction or PostAction. In the examples, they call an example method:
foo = Program('foo.c')
AddPreAction(foo, 'pre_action')
This calls 'pre_action' before 'foo.c' is compiled and linked. However, there is no explanation about what format 'pre_action' must take.
I have discovered that there are several pre-built actions, such as 'Copy'. However, I cannot find the source of 'Copy' to reverse-engineer it and write my own.
Can anyone point me to a guide as to how to create my own Actions? I either need an interface for calling my own method such as 'pre_action' above, or a guide for writing an Action class.
Really, even just some example code that is actually complete enough to use would be helpful...
The manpage section Action Objects lists the types of things that can be passed to the Action factory function to create an action; that is also what you pass to AddPostAction and AddPreAction as the second argument - that is, either an Action already made by a previous call to Action, or something that can be converted into one like a command string, or a list of such, or a function with appropriate signature. Pre/Post will simply call the Action function with that argument. So in that section, where there's an example with a call to Action, you could just plug that argument into AddPreAction, or you could save the result of calling Action and give that as the argument to AddPreAction.
The amount of flexibility here makes it a little tricky to document concisely.
(btw the source to Copy is a function called copy_func but you probably don't want to use that form because it's a couple of extra levels of abstraction you won't need)

optional name argument in tensor flow API's

I am beginner in tesnor flow. Trying to understand about various API's and features. For most of the API's in tensor flow has name argument like shown below
# Create a variable.
w = tf.Variable(0, name="abc">)
In most of the programming example "w" is used. I am not getting when name optional argument is used for example how "abc" is used either inside the variable function and how it used in following lines after executing above statement. Kindly help.
Name argument is used in most cases so you can restore your variables from a saved checkpoint. Check this https://www.tensorflow.org/guide/saved_model . Furthermore it is helpful for finding your variables or operations that are visualized in the graph through tensorboard.

What's the name arguments in string_to_hash_bucket_fast for?

Just new to tensorflow. When checking impl for feature_column.categorical_column_with_hash_bucket, I found this code:
sparse_id_values = string_ops.string_to_hash_bucket_fast(sparse_values,
self.hash_bucket_size, name='lookup')
Not sure why use name='lookup' here, is it related with lookup_ops.py? Documentation specified in tf.string_to_hash_bucket_fast:
name: A name for the operation (optional).
But not quite understand, trying to go deeper in source code, found it's wrapped in go/wrapper in a interface, even can't find a detailed algo impl. Any suggestions?
tf.string_to_hash_bucket_fast() creates an actual op in the graph. It's called StringToHashBucketFast in the native implementation, see the source code in tensorflow/core/kernels/string_to_hash_bucket_op.cc. In particular, it has a gradient. So the name can be helpful to recognize this op in the graph, for example in tensorboard.
The name lookup in this place explains the meaning of sparse_id_values: it's a conversion of sparse_values to ids (hashes).

How to implement custom environment in keras-rl / OpenAI GYM?

I'm a complete newbie to Reinforcement Learning and have been searching for a framework/module to easily navigate this treacherous terrain. In my search I've come across two modules keras-rl & OpenAI GYM.
I can get both of them two work on the examples they have shared on their WIKIs but they come with predefined environments and have little or no information on how to setup my own custom environment.
I would be really thankful if anyone could point me towards a tutorial or just explain it to me on how can i setup a non-game environment?
I've been working on these libraries for some time and can share some of my experiments.
Let us first consider as an example of custom environment a text environment, https://github.com/openai/gym/blob/master/gym/envs/toy_text/hotter_colder.py
For a custom environment, a couple of things should be defined.
Constructor__init__ method
Action space
Observation space (see https://github.com/openai/gym/tree/master/gym/spaces for all available gym spaces (it's a kind of data structure))
_seed method (not sure that it's mandatory)
_step method accepting action as a param and returning observation (state after action), reward (for transition to new observational state), done (boolean flag), and some optional additional info.
_reset method that implements logic of fresh start of episode.
Optionally, you can create a _render method with something like
def _render(self, mode='human', **kwargs):
outfile = StringIO() if mode == 'ansi' else sys.stdout
outfile.write('State: ' + repr(self.state) + ' Action: ' + repr(self.action_taken) + '\n')
return outfile
And also, for better code flexibility, you can define logic of your reward in _get_reward method and changes to observation space from taking action in _take_action method.

How to view and interpret the output of lda model using gensim

I am able to create the lda model and save it. Now I am trying load the model, and pass a new document
lda = LdaModel.load('..\\models\\lda_v0.1.model')
doc_lda = lda[new_doc_term_matrix]
print(doc_lda )
On printing the doc_lda I am getting the object. <gensim.interfaces.TransformedCorpus object at 0x000000F82E4BB630>
However I want to get the topic words associated with it. What is the method I have to use. I was referring to this.
Not sure if this is still relevant, but have you tried get_document_topics()? Though I assume that would only work if you've updated your LDA model using update().
I don't think there is anything wrong with your code - the "Usage example" from the documentation link you posted uses doc2bow which returns a sparse vector - I don't know what new_doc_term_matrix consists of, but I'll assume it worked fine.
You might want to look at this stackoverflow question: you want to print an "object" - that isn't printable, the data you want is somewhere in the object, and that in itself is printable.
Alternatively, you can also use your IDE's capabilities - the Variable explorer in Spyder, for example - to click yourself into the objects and get the info you need.
For more info on similarity analysis with gensim, see this tutorial.
Use this
doc_lda.print_topics(-1)

Resources