Theano Study Notes (6)-loading and storage, conditions, and theano Study Notes

Source: Internet
Author: User
Tags theano

Theano Study Notes (6)-loading and storage, conditions, and theano Study Notes

Load and save

The pickle mechanism is used to save class entities and reload them in Python. Many Theano objects can be serialized (or deserialized). However, pickle has the limitation that the code or data of the serialized category instance is not saved at the same time. Therefore, re-loading the previous version of the class may cause problems.

Therefore, different mechanisms need to be sought based on the expected storage and reload time.

Theano's pickle is feasible for short-term (such as temporary files and network transcription.

For long-term (for example, saving models from experiments), it should not depend on Theano's pickle object.

We recommend that you save and load the underlying shared objects in any other Python project.

 

Pickle Basics

Pickle and cPickle have similar functions, but cPickle uses C encoding, which is faster.

You can use cPickle. dump to serialize (or save or pickle) an object as a file.

 

importcPicklef= file('obj.save', 'wb')cPickle.dump(my_obj,f, protocol=cPickle.HIGHEST_PROTOCOL)f.close()


CPickle. HIGHEST_PROTOCOL is used to accelerate the process of saving objects.

The 'B' binary mode is used to maintain portability between Unix and Windows systems.

 

Use cPickle. load to deserialize (or load, or unpickle) the file)

f= file('obj.save', 'rb')loaded_obj= cPickle.load(f)f.close()


Multiple objects can be pickle to the same file at the same time:

f= file('objects.save', 'wb')forobj in [obj1, obj2, obj3]:    cPickle.dump(obj, f,protocol=cPickle.HIGHEST_PROTOCOL)f.close()


You can also load data in the same order:

f= file('objects.save', 'rb')loaded_objects= []fori in range(3):    loaded_objects.append(cPickle.load(f))f.close()


Short-term serialization

If you are confident, the entire pickle model is a good solution.

In this case, you perform the same save and reload operations in the project, or the class has been running stably for a long time.

You can define _ getstate _ method and _ setstate _ to control which pickle is saved from the project.

If the model class contains a link to a dataset in use and does not want to pickle each model instance, the above control method will be very practical.

def__getstate__(self):    state = dict(self.__dict__)    del state['training_set']    return state def__setstate__(self, d):    self.__dict__.update(d)self.training_set =cPickle.load(file(self.training_set_file, 'rb'))


Long-term serialization

If the class to be saved is unstable, such as creating or deleting a function or renaming a class member, only the immutable part of the class should be saved or loaded.

Define _ getstate _ method and _ setstate __

For example, you only want to save the weight matrix W and bias item B:

def__getstate__(self):    return (self.W, self.b) def__setstate__(self, state):    W, b = state    self.W = Wself.b = b

 

If the following functions are updated to change the variable name, even if W and B are renamed to weights and bias, the previous pickle file is still available:

def__getstate__(self):    return (self.weights, self.bias) def__setstate__(self, state):    W, b = state    self.weights = Wself.bias = b

 

Condition

-IfElse and Switch

-Switch is more common than ifelse because switch is a bit-by-bit operation.

-Switch calculates both output variables, so it is slower than ifelse (only one ).

 

from theano import tensor as Tfrom theano.ifelse import ifelseimporttheano, time, numpy a,b= T.scalars('a', 'b')x,y= T.matrices('x', 'y') z_switch= T.switch(T.lt(a, b), T.mean(x), T.mean(y))z_lazy= ifelse(T.lt(a, b), T.mean(x), T.mean(y)) f_switch= theano.function([a, b, x, y], z_switch,                   mode=theano.Mode(linker='vm'))f_lazyifelse= theano.function([a, b, x, y], z_lazy,                   mode=theano.Mode(linker='vm')) val1= 0.val2= 1.big_mat1= numpy.ones((10000, 1000))big_mat2= numpy.ones((10000, 1000)) n_times= 10 tic= time.clock()fori in xrange(n_times):    f_switch(val1, val2, big_mat1, big_mat2)print'time spent evaluating both values %f sec' % (time.clock() - tic) tic= time.clock()fori in xrange(n_times):    f_lazyifelse(val1, val2, big_mat1,big_mat2)print'time spent evaluating one value %f sec' % (time.clock() - tic)


Test Results

time spent evaluating both values 0.200000 sectime spent evaluating one value 0.110000 sec

It can be seen that ifelse is indeed faster than doubled, but vm or cvm must be used as the Linker, and cvm will be used as the default Linker in the future.


Welcome to the discussion and follow up on this blog, Weibo, and zhihu personal homepage for further updates ~

Reprinted, please respect the work of the author and keep the above text and link of the article completely. Thank you for your support!


Example or format

[Cultivate study habits of sixth-grade students ]]
-------- About the students' habit of taking notes

As a teacher, listening to the lessons of fellow teachers is not only a basic requirement of teaching conventions, but also can learn from each other and learn from each other. The teaching of a course involves the processing of teaching content, the application of teaching methods, the arrangement of classroom structure, bilateral activities of teachers and students, and the language, blackboard writing, and teaching mode of instructors. During the lesson, we recorded the information in a timely and accurate manner, which is conducive to further research and study after class and provides a basis for comprehensive and accurate evaluation of a class.

In fact, it is of great help for learners to learn to take notes in a timely manner, whether in lectures or in reports. However, in the process of listening, it is unrealistic to keep a full set of words. Therefore, you must learn the methods and selectively record them to facilitate review.

For students in the sixth-grade graduating class, both age and psychology are in adolescence. The children at this stage are very distinctive and have a strong ability to receive new things, in the course of learning, some methods can be accepted by teachers and professors. Students urgently need to find a method suitable for their own learning, at the same time, according to the Teaching Requirements and learning content, I started to cultivate the habit of taking notes for students in our class in the second half of this semester. Since half a semester, I found that students are very happy to accept this learning method, the effect is obvious.

The following methods are provided to students:

1. to train students to take notes, they must first start from the meaning of the notes and tell the students the advantages of taking notes so that they can have the desire to remember. Teachers should remind students to keep records in a timely manner every time they need to record the records, especially those who refuse to listen to the class, which makes it easier to improve the learning performance of poor students.

2. deal with the relationship between listening and recording, both listen to the good and do not miss important places and record them.

3. Make notes before class. Prepare two pens of different colors, and record the knowledge at a glance, so that you can easily review and consolidate your knowledge.

4. Submit the Notes to the methods.

1. mathematical symbols to help. Make good use of textbook resources and some common mathematical symbols to help you quickly record data. Note taking by pupils is significantly different from taking note by students. In the English textbooks for the next semester of the fifth grade, grammar knowledge about the tense has been involved, mainly in the current tense. generally in the present tense, the past tense, and the general future, there are two kinds of tense in different units, the instructor must first tell the students how to use different symbols to mark different tenses. The students must be aware and discover different tenses for labeling analysis.

2. Add key words and notes. In addition to grammar knowledge, students also need to remember words and phrases and perform different annotation methods based on different parts of speech. For example, the key words or keywords in phrases are recorded by adding points, some words can be annotated with accepted English words.

3. Opening up accumulated corners. Students are required to prepare a notebook dedicated to collecting vocabulary and phrases to collect the words they encounter in different parts of speech during their normal processes. The nature of the words they identify is added to the location of the corresponding words, for example: fix nouns on several pages, add nouns on these pages, and ask the teacher if they are inaccurate. Teachers can take incentives to encourage students' enthusiasm.

4. Document record. That is to say, in the blank area of the corresponding row in the textbook, record the important and difficult points explained by the teacher in a timely manner, and collect excerpts after class. You can also make a question mark there if you do not understand the sentence in the text during the preview. If the teacher explains it in time, record it; otherwise, ask for advice after class.

Poor memory. Smart people cannot develop good learning habits from an early age, and the quality of learning will be greatly reduced. As a teacher, it is especially important to cultivate students' good habit of taking notes if they want to get twice the result with half the effort.

========================================================== ========================================================== ====

The above is the learning pen... the remaining full text>

Six-person Study Notes

You need to send it. Please check it in time

_____________________ Redhouse

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.