numpy.float32

Here are the examples of the python api numpy.float32 taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

200 Examples 7

Example 1

Project: NMT
Source File: LM_model.py
View license
    def  __init__(self,
                  cost_layer = None,
                  sample_fn = None,
                  valid_fn = None,
                  noise_fn = None,
                  clean_before_noise_fn = False,
                  clean_noise_validation=True,
                  weight_noise_amount = 0,
                  indx_word="/data/lisa/data/PennTreebankCorpus/dictionaries.npz",
                  need_inputs_for_generating_noise=False,
                  indx_word_src=None,
                  character_level = False,
                  exclude_params_for_norm=None,
                  rng = None):
        """
        Constructs a model, that respects the interface required by the
        trainer class.

        :type cost_layer: groundhog layer
        :param cost_layer: the cost (last) layer of the model

        :type sample_fn: function or None
        :param sample_fn: function used to sample from the model

        :type valid_fn: function or None
        :param valid_fn: function used to compute the validation error on a
            minibatch of examples

        :type noise_fn: function or None
        :param noise_fn: function called to corrupt an input (that
            potentially will be denoised by the model)

        :type clean_before_noise_fn: bool
        :param clean_before_noise_fn: If the weight noise should be removed
            before calling the `noise_fn` to corrupt some input

        :type clean_noise_validation: bool
        :param clean_noise_validation: If the weight noise should be removed
            before calling the validation function

        :type weight_noise_amount: float or theano scalar
        :param weight_noise_amount: weight noise scale (standard deviation
            of the Gaussian from which it is sampled)

        :type indx_word: string or None
        :param indx_word: path to the file describing how to match indices
            to words (or characters)

        :type need_inputs_for_generating_noise: bool
        :param need_inputs_for_generating_noise: flag saying if the shape of
            the inputs affect the shape of the weight noise that is generated at
            each step

        :type indx_word_src: string or None
        :param indx_word_src: similar to indx_word (but for the source
            language

        :type character_level: bool
        :param character_level: flag used when sampling, saying if we are
            running the model on characters or words

        :type excluding_params_for_norm: None or list of theano variables
        :param excluding_params_for_norm: list of parameters that should not
            be included when we compute the norm of the gradient (for norm
            clipping). Usually the output weights if the output layer is
            large

        :type rng: numpy random generator
        :param rng: numpy random generator

        """
        super(LM_Model, self).__init__(output_layer=cost_layer,
                                       sample_fn=sample_fn,
                                       indx_word=indx_word,
                                       indx_word_src=indx_word_src,
                                       rng=rng)
        if exclude_params_for_norm is None:
            self.exclude_params_for_norm = []
        else:
            self.exclude_params_for_norm = exclude_params_for_norm
        self.need_inputs_for_generating_noise=need_inputs_for_generating_noise
        self.cost_layer = cost_layer
        self.validate_step = valid_fn
        self.clean_noise_validation = clean_noise_validation
        self.noise_fn = noise_fn
        self.clean_before = clean_before_noise_fn
        self.weight_noise_amount = weight_noise_amount
        self.character_level = character_level

        self.valid_costs = ['cost','ppl']
        # Assume a single cost
        # We need to merge these lists
        state_below = self.cost_layer.state_below
        if hasattr(self.cost_layer, 'mask') and self.cost_layer.mask:
            num_words = TT.sum(self.cost_layer.mask)
        else:
            num_words = TT.cast(state_below.shape[0], 'float32')
        scale = getattr(self.cost_layer, 'cost_scale', numpy.float32(1))
        if not scale:
            scale = numpy.float32(1)
        scale *= numpy.float32(numpy.log(2))

        grad_norm = TT.sqrt(sum(TT.sum(x**2)
            for x,p in zip(self.param_grads, self.params) if p not in
                self.exclude_params_for_norm))
        new_properties = [
                ('grad_norm', grad_norm),
                ('log2_p_word', self.train_cost / num_words / scale),
                ('log2_p_expl', self.cost_layer.cost_per_sample.mean() / scale)]
        self.properties += new_properties

        if len(self.noise_params) >0 and weight_noise_amount:
            if self.need_inputs_for_generating_noise:
                inps = self.inputs
            else:
                inps = []
            self.add_noise = theano.function(inps,[],
                                             name='add_noise',
                                             updates = [(p,
                                                 self.trng.normal(shp_fn(self.inputs),
                                                     avg =0,
                                                     std=weight_noise_amount,
                                                     dtype=p.dtype))
                                                 for p, shp_fn in
                                                        zip(self.noise_params,
                                                         self.noise_params_shape_fn)],
                                            on_unused_input='ignore')
            self.del_noise = theano.function(inps,[],
                                             name='del_noise',
                                             updates=[(p,
                                                       TT.zeros(shp_fn(self.inputs),
                                                                p.dtype))
                                                      for p, shp_fn in
                                                      zip(self.noise_params,
                                                          self.noise_params_shape_fn)],
                                            on_unused_input='ignore')
        else:
            self.add_noise = None
            self.del_noise = None

Example 2

Project: LV_groundhog
Source File: LM_model.py
View license
    def  __init__(self,
                  cost_layer = None,
                  sample_fn = None,
                  valid_fn = None,
                  noise_fn = None,
                  clean_before_noise_fn = False,
                  clean_noise_validation=True,
                  weight_noise_amount = 0,
                  indx_word="/data/lisa/data/PennTreebankCorpus/dictionaries.npz",
                  need_inputs_for_generating_noise=False,
                  indx_word_src=None,
                  character_level = False,
                  exclude_params_for_norm=None,
                  rng = None):
        """
        Constructs a model, that respects the interface required by the
        trainer class.

        :type cost_layer: groundhog layer
        :param cost_layer: the cost (last) layer of the model

        :type sample_fn: function or None
        :param sample_fn: function used to sample from the model

        :type valid_fn: function or None
        :param valid_fn: function used to compute the validation error on a
            minibatch of examples

        :type noise_fn: function or None
        :param noise_fn: function called to corrupt an input (that
            potentially will be denoised by the model)

        :type clean_before_noise_fn: bool
        :param clean_before_noise_fn: If the weight noise should be removed
            before calling the `noise_fn` to corrupt some input

        :type clean_noise_validation: bool
        :param clean_noise_validation: If the weight noise should be removed
            before calling the validation function

        :type weight_noise_amount: float or theano scalar
        :param weight_noise_amount: weight noise scale (standard deviation
            of the Gaussian from which it is sampled)

        :type indx_word: string or None
        :param indx_word: path to the file describing how to match indices
            to words (or characters)

        :type need_inputs_for_generating_noise: bool
        :param need_inputs_for_generating_noise: flag saying if the shape of
            the inputs affect the shape of the weight noise that is generated at
            each step

        :type indx_word_src: string or None
        :param indx_word_src: similar to indx_word (but for the source
            language

        :type character_level: bool
        :param character_level: flag used when sampling, saying if we are
            running the model on characters or words

        :type excluding_params_for_norm: None or list of theano variables
        :param excluding_params_for_norm: list of parameters that should not
            be included when we compute the norm of the gradient (for norm
            clipping). Usually the output weights if the output layer is
            large

        :type rng: numpy random generator
        :param rng: numpy random generator

        """
        super(LM_Model, self).__init__(output_layer=cost_layer,
                                       sample_fn=sample_fn,
                                       indx_word=indx_word,
                                       indx_word_src=indx_word_src,
                                       rng=rng)
        if exclude_params_for_norm is None:
            self.exclude_params_for_norm = []
        else:
            self.exclude_params_for_norm = exclude_params_for_norm
        self.need_inputs_for_generating_noise=need_inputs_for_generating_noise
        self.cost_layer = cost_layer
        self.validate_step = valid_fn
        self.clean_noise_validation = clean_noise_validation
        self.noise_fn = noise_fn
        self.clean_before = clean_before_noise_fn
        self.weight_noise_amount = weight_noise_amount
        self.character_level = character_level

        self.valid_costs = ['cost','ppl']
        # Assume a single cost
        # We need to merge these lists
        state_below = self.cost_layer.state_below
        if hasattr(self.cost_layer, 'mask') and self.cost_layer.mask:
            num_words = TT.sum(self.cost_layer.mask)
        else:
            num_words = TT.cast(state_below.shape[0], 'float32')
        scale = getattr(self.cost_layer, 'cost_scale', numpy.float32(1))
        if not scale:
            scale = numpy.float32(1)
        scale *= numpy.float32(numpy.log(2))

        grad_norm = TT.sqrt(sum(TT.sum(x**2)
            for x,p in zip(self.param_grads, self.params) if p not in
                self.exclude_params_for_norm))
        new_properties = [
                ('grad_norm', grad_norm),
                ('log2_p_word', self.train_cost / num_words / scale),
                ('log2_p_expl', self.cost_layer.cost_per_sample.mean() / scale)]
        self.properties += new_properties

        if len(self.noise_params) >0 and weight_noise_amount:
            if self.need_inputs_for_generating_noise:
                inps = self.inputs
            else:
                inps = []
            self.add_noise = theano.function(inps,[],
                                             name='add_noise',
                                             updates = [(p,
                                                 self.trng.normal(shp_fn(self.inputs),
                                                     avg =0,
                                                     std=weight_noise_amount,
                                                     dtype=p.dtype))
                                                 for p, shp_fn in
                                                        zip(self.noise_params,
                                                         self.noise_params_shape_fn)],
                                            on_unused_input='ignore')
            self.del_noise = theano.function(inps,[],
                                             name='del_noise',
                                             updates=[(p,
                                                       TT.zeros(shp_fn(self.inputs),
                                                                p.dtype))
                                                      for p, shp_fn in
                                                      zip(self.noise_params,
                                                          self.noise_params_shape_fn)],
                                            on_unused_input='ignore')
        else:
            self.add_noise = None
            self.del_noise = None

Example 3

Project: HPGL-GUI
Source File: gui.py
View license
    def saveCube(self):
        index = self.getIndex()
        row = self.getRow()
        
        eclipseFilter = "Eclipse (*.inc)"
        gslibFilter = "GSLIB (*.gslib)"
        numpyFilter = "Numpy (*.npy)"
        
        fileDialog = QtGui.QFileDialog()
        fileDialog.setNameFilters((
                                   eclipseFilter, 
                                   gslibFilter,
                                   numpyFilter
                                 ))
        fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
        fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
        
        if fileDialog.exec_():
            filter = fileDialog.selectedNameFilter()
            fname = fileDialog.selectedFiles()[0]
        else:
            return
        #fname = QtGui.QFileDialog.getSaveFileName(self, 'Save as ...')[0]
        
        if fname and self.isIndexCont(index):
            if filter == eclipseFilter:
                fname += '.inc'
                
                try:
                    write_property(self.contCubes.property(row), fname,
                                   self.contCubes.name(row), numpy.float32(self.contCubes.undefValue(row)),
                                   self.contCubes.indicators(row))
                    self.algorithmText.setText(self.__tr('Cube was saved'))
    
                except:
                    self.algorithmText.setText(self.__tr('Error saving cube'))

            if filter == gslibFilter:
                fname += '.gslib'
                try:
                    write_gslib_property(self.contCubes.property(row), fname, 
                                         self.contCubes.name(row), numpy.float32(self.contCubes.undefValue(row))
                                        )
                    self.algorithmText.setText(self.__tr('Cube was saved'))
                except:
                    self.algorithmText.setText(self.__tr('Error saving cube'))
            
            if filter == numpyFilter:
                try:
                    numpy.save(fname, self.contCubes.property(row))
                    self.algorithmText.setText(self.__tr('Cube was saved'))
                except:
                    self.algorithmText.setText(self.__tr('Error saving cube'))

        elif fname and self.isIndexInd(index):
            if filter == eclipseFilter:
                fname += '.inc'
                
                try:
                    write_property(self.indCubes.property(row), fname,
                                   self.indCubes.name(row), numpy.float32(self.indCubes.undefValue(row)),
                                   list(self.indCubes.indicators(row)))
                    self.algorithmText.setText(self.__tr('Cube was saved'))
    
                except:
                    self.algorithmText.setText(self.__tr('Error saving cube'))
            if filter == gslibFilter:
                fname += '.gslib'
                try:
                    write_gslib_property(self.indCubes.property(row), fname, 
                                         self.indCubes.name(row), numpy.float32(self.indCubes.undefValue(row)),
                                         list(self.indCubes.indicators(row))
                                        )
                    self.algorithmText.setText(self.__tr('Cube was saved'))
                except:
                    self.algorithmText.setText(self.__tr('Error saving cube'))
            
            if filter == numpyFilter:
                try:
                    numpy.save(fname, self.indCubes.property(row))
                    self.algorithmText.setText(self.__tr('Cube was saved'))
                except:
                    self.algorithmText.setText(self.__tr('Error saving cube'))

Example 4

Project: NMT-Coverage
Source File: LM_model.py
View license
    def  __init__(self,
                  cost_layer = None,
                  sample_fn = None,
                  valid_fn = None,
                  noise_fn = None,
                  clean_before_noise_fn = False,
                  clean_noise_validation=True,
                  weight_noise_amount = 0,
                  indx_word="/data/lisa/data/PennTreebankCorpus/dictionaries.npz",
                  need_inputs_for_generating_noise=False,
                  indx_word_src=None,
                  character_level = False,
                  exclude_params_for_norm=None,
                  rng = None):
        """
        Constructs a model, that respects the interface required by the
        trainer class.

        :type cost_layer: groundhog layer
        :param cost_layer: the cost (last) layer of the model

        :type sample_fn: function or None
        :param sample_fn: function used to sample from the model

        :type valid_fn: function or None
        :param valid_fn: function used to compute the validation error on a
            minibatch of examples

        :type noise_fn: function or None
        :param noise_fn: function called to corrupt an input (that
            potentially will be denoised by the model)

        :type clean_before_noise_fn: bool
        :param clean_before_noise_fn: If the weight noise should be removed
            before calling the `noise_fn` to corrupt some input

        :type clean_noise_validation: bool
        :param clean_noise_validation: If the weight noise should be removed
            before calling the validation function

        :type weight_noise_amount: float or theano scalar
        :param weight_noise_amount: weight noise scale (standard deviation
            of the Gaussian from which it is sampled)

        :type indx_word: string or None
        :param indx_word: path to the file describing how to match indices
            to words (or characters)

        :type need_inputs_for_generating_noise: bool
        :param need_inputs_for_generating_noise: flag saying if the shape of
            the inputs affect the shape of the weight noise that is generated at
            each step

        :type indx_word_src: string or None
        :param indx_word_src: similar to indx_word (but for the source
            language

        :type character_level: bool
        :param character_level: flag used when sampling, saying if we are
            running the model on characters or words

        :type excluding_params_for_norm: None or list of theano variables
        :param excluding_params_for_norm: list of parameters that should not
            be included when we compute the norm of the gradient (for norm
            clipping). Usually the output weights if the output layer is
            large

        :type rng: numpy random generator
        :param rng: numpy random generator

        """
        super(LM_Model, self).__init__(output_layer=cost_layer,
                                       sample_fn=sample_fn,
                                       indx_word=indx_word,
                                       indx_word_src=indx_word_src,
                                       rng=rng)
        if exclude_params_for_norm is None:
            self.exclude_params_for_norm = []
        else:
            self.exclude_params_for_norm = exclude_params_for_norm
        self.need_inputs_for_generating_noise=need_inputs_for_generating_noise
        self.cost_layer = cost_layer
        self.validate_step = valid_fn
        self.clean_noise_validation = clean_noise_validation
        self.noise_fn = noise_fn
        self.clean_before = clean_before_noise_fn
        self.weight_noise_amount = weight_noise_amount
        self.character_level = character_level

        self.valid_costs = ['cost','ppl']
        # Assume a single cost
        # We need to merge these lists
        state_below = self.cost_layer.state_below
        if hasattr(self.cost_layer, 'mask') and self.cost_layer.mask:
            num_words = TT.sum(self.cost_layer.mask)
        else:
            num_words = TT.cast(state_below.shape[0], 'float32')
        scale = getattr(self.cost_layer, 'cost_scale', numpy.float32(1))
        if not scale:
            scale = numpy.float32(1)
        scale *= numpy.float32(numpy.log(2))

        grad_norm = TT.sqrt(sum(TT.sum(x**2)
            for x,p in zip(self.param_grads, self.params) if p not in
                self.exclude_params_for_norm))
        new_properties = [
                ('grad_norm', grad_norm),
                ('log2_p_word', self.train_cost / num_words / scale),
                ('log2_p_expl', self.cost_layer.cost_per_sample.mean() / scale)]
        self.properties += new_properties

        if len(self.noise_params) >0 and weight_noise_amount:
            if self.need_inputs_for_generating_noise:
                inps = self.inputs
            else:
                inps = []
            self.add_noise = theano.function(inps,[],
                                             name='add_noise',
                                             updates = [(p,
                                                 self.trng.normal(shp_fn(self.inputs),
                                                     avg =0,
                                                     std=weight_noise_amount,
                                                     dtype=p.dtype))
                                                 for p, shp_fn in
                                                        zip(self.noise_params,
                                                         self.noise_params_shape_fn)],
                                            on_unused_input='ignore')
            self.del_noise = theano.function(inps,[],
                                             name='del_noise',
                                             updates=[(p,
                                                       TT.zeros(shp_fn(self.inputs),
                                                                p.dtype))
                                                      for p, shp_fn in
                                                      zip(self.noise_params,
                                                          self.noise_params_shape_fn)],
                                            on_unused_input='ignore')
        else:
            self.add_noise = None
            self.del_noise = None

Example 5

Project: horus
Source File: scene_view.py
View license
    def _draw_machine(self):
        glEnable(GL_BLEND)
        machine_model_path = profile.settings['machine_model_path']
        glEnable(GL_CULL_FACE)

        # Draw Platform
        if machine_model_path in self._platform_mesh:
            try:  # TODO: Fix this. If not in the Scanning workbench, _draw_machine() fails.
                self._platform_mesh[machine_model_path]._mesh.vbo.release()
            except:
                pass

        mesh = mesh_loader.load_mesh(machine_model_path)
        if mesh is not None:
            self._platform_mesh[machine_model_path] = mesh
        else:
            self._platform_mesh[machine_model_path] = None
        self._platform_mesh[machine_model_path]._draw_offset = numpy.array(
            [0, 0, 8.05], numpy.float32)
        glColor4f(0.6, 0.6, 0.6, 0.5)
        self._object_shader.bind()
        self._render_object(self._platform_mesh[machine_model_path])
        self._object_shader.unbind()
        glDisable(GL_CULL_FACE)

        glDepthMask(False)

        machine_shape = profile.settings['machine_shape']

        if machine_shape == 'Circular':
            size = numpy.array([profile.settings['roi_diameter'],
                                profile.settings['roi_diameter'],
                                profile.settings['roi_height']], numpy.float32)
        elif machine_shape == 'Rectangular':
            size = numpy.array([profile.settings['roi_width'],
                                profile.settings['roi_depth'],
                                profile.settings['roi_height']], numpy.float32)

        if self._view_roi:
            polys = profile.get_size_polygons(size, machine_shape)
            height = profile.settings['roi_height']

            # Draw the sides of the build volume.
            glBegin(GL_QUADS)
            for n in xrange(0, len(polys[0])):
                if machine_shape == 'Rectangular':
                    if n % 2 == 0:
                        glColor4ub(5, 171, 231, 96)
                    else:
                        glColor4ub(5, 171, 231, 64)
                elif machine_shape == 'Circular':
                    glColor4ub(5, 171, 231, 96)
                    # glColor4ub(200, 200, 200, 150)

                glVertex3f(polys[0][n][0], polys[0][n][1], height)
                glVertex3f(polys[0][n][0], polys[0][n][1], 0)
                glVertex3f(polys[0][n - 1][0], polys[0][n - 1][1], 0)
                glVertex3f(polys[0][n - 1][0], polys[0][n - 1][1], height)
            glEnd()

            # Draw bottom and top of build volume.
            glColor4ub(5, 171, 231, 150)  # 128)
            # glColor4ub(200, 200, 200, 200)
            glBegin(GL_TRIANGLE_FAN)
            for p in polys[0][::-1]:
                glVertex3f(p[0], p[1], 0)
            glEnd()
            glBegin(GL_TRIANGLE_FAN)
            for p in polys[0][::-1]:
                glVertex3f(p[0], p[1], height)
            glEnd()

            quadric = gluNewQuadric()
            gluQuadricNormals(quadric, GLU_SMOOTH)
            gluQuadricTexture(quadric, GL_TRUE)
            glColor4ub(0, 100, 200, 150)

            gluCylinder(quadric, 6, 6, 1, 32, 16)
            gluDisk(quadric, 0.0, 6, 32, 1)

            glTranslate(0, 0, height - 1)
            gluDisk(quadric, 0.0, 6, 32, 1)
            gluCylinder(quadric, 6, 6, 1, 32, 16)
            glTranslate(0, 0, -height + 1)

        polys = profile.get_machine_size_polygons(profile.settings["machine_shape"])

        # Draw checkerboard
        if self._platform_texture is None:
            self._platform_texture = opengl_helpers.load_gl_texture('checkerboard.png')
            glBindTexture(GL_TEXTURE_2D, self._platform_texture)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
        glColor4f(1, 1, 1, 0.5)
        glBindTexture(GL_TEXTURE_2D, self._platform_texture)
        glEnable(GL_TEXTURE_2D)
        glBegin(GL_TRIANGLE_FAN)
        for p in polys[0]:
            glTexCoord2f(p[0] / 20, p[1] / 20)
            glVertex3f(p[0], p[1], 0)
        glEnd()
        glDisable(GL_TEXTURE_2D)

        glDepthMask(True)
        glDisable(GL_BLEND)

Example 6

Project: nupic.vision
Source File: Convolution.py
View license
  def _processImage(self, image, bbox):
    """Return a single image, or a list containing one or more images.

    @param image -- The image to process.
    """
    BaseFilter.process(self, image)

    inWidth, inHeight = image.size

    # Get output dims and buffers (cached)
    outWidth, outHeight, inBuffer, outBuffer, mask = self._prepare(image.size)

    # Ask the sub-class the build the filter bank
    self._buildFilterBank()

    inputOffset  = 0
    outputOffset = 0

    data = image.split()[0]
    inputVector = numpy.asarray(data, dtype=numpy.float32)

    inputVector.shape = (inHeight, inWidth)

    # If we are using "color-key" mode, then detect the value of
    # the upper-left pixel and use it as the value of
    # 'offImagePixelValue'
    if self._offImagePixelValue in ('colorKey', u'colorKey'):
      offImagePixelValue = inputVector[0, 0]
    else:
      offImagePixelValue = self._offImagePixelValue

    result = []

    # Compute the convolution responses

    # Determine proper input/output dimensions
    outputSize = outHeight * outWidth * self._outputPlaneCount

    # Locate correct portion of output
    outputVector = numpy.zeros((outHeight,
                                outWidth,
                                self._outputPlaneCount),
                                dtype=numpy.float32)
    outputVector.shape = (self._outputPlaneCount, outHeight, outWidth)


    # Compute the bounding box to use for our C implementation
    imageBox = numpy.array([0, 0, inWidth, inHeight], dtype=numpy.int32)

    ## --- DEBUG CODE ----
    #global id
    #o = inputVector
    #f = os.path.abspath('convolution_input_%d.txt' % id)
    #print f
    #numpy.savetxt(f, o)
    #id += 1
    ##from dbgp.client import brk; brk(port=9019)
    ## --- DEBUG CODE END ----

    # Call the fast convolution C code
    self._convolve(inputVector,
                   bbox,
                   imageBox,
                   outputVector,
                   offImagePixelValue,
                   inBuffer,
                   outBuffer)

    outputVector = numpy.rollaxis(outputVector, 0, 3)
    outputVector = outputVector.reshape(outWidth * outHeight,
                                        self._outputPlaneCount).flatten()
    assert outputVector.dtype == numpy.float32

    locationCount = len(outputVector) / self._outputPlaneCount
    response = outputVector.reshape(locationCount, self._outputPlaneCount)

    ## --- DEBUG CODE ----
    #global id
    #o = outputVector.flatten()
    ##print outputVector.shape, len(o)
    #f = os.path.abspath('convolution_output_%d.txt' % id)
    #print f
    #numpy.savetxt(f, o)
    #id += 1
    ##from dbgp.client import brk; brk(port=9019)
    ## --- DEBUG CODE END ----

    # Convert the reponses to images
    result = []
    for i in range(response.shape[1]):
      newImage = Image.new('L', (outWidth, outHeight))
      #data = (self._gainConstant * 255.0 * response[:,i]).clip(min=0.0, max=255.0).astype(numpy.uint8)
      data = (255.0 * response[:,i]).clip(min=0.0, max=255.0).astype(numpy.uint8)
      newImage.putdata([uint(p) for p in data])
      newImage.putalpha(mask)
      result.append(newImage)

    return (result, outputVector)

Example 7

Project: NMT
Source File: LM_model.py
View license
    def  __init__(self,
                  cost_layer = None,
                  sample_fn = None,
                  valid_fn = None,
                  noise_fn = None,
                  clean_before_noise_fn = False,
                  clean_noise_validation=True,
                  weight_noise_amount = 0,
                  indx_word="/data/lisa/data/PennTreebankCorpus/dictionaries.npz",
                  need_inputs_for_generating_noise=False,
                  indx_word_src=None,
                  character_level = False,
                  exclude_params_for_norm=None,
                  rng = None):
        """
        Constructs a model, that respects the interface required by the
        trainer class.

        :type cost_layer: groundhog layer
        :param cost_layer: the cost (last) layer of the model

        :type sample_fn: function or None
        :param sample_fn: function used to sample from the model

        :type valid_fn: function or None
        :param valid_fn: function used to compute the validation error on a
            minibatch of examples

        :type noise_fn: function or None
        :param noise_fn: function called to corrupt an input (that
            potentially will be denoised by the model)

        :type clean_before_noise_fn: bool
        :param clean_before_noise_fn: If the weight noise should be removed
            before calling the `noise_fn` to corrupt some input

        :type clean_noise_validation: bool
        :param clean_noise_validation: If the weight noise should be removed
            before calling the validation function

        :type weight_noise_amount: float or theano scalar
        :param weight_noise_amount: weight noise scale (standard deviation
            of the Gaussian from which it is sampled)

        :type indx_word: string or None
        :param indx_word: path to the file describing how to match indices
            to words (or characters)

        :type need_inputs_for_generating_noise: bool
        :param need_inputs_for_generating_noise: flag saying if the shape of
            the inputs affect the shape of the weight noise that is generated at
            each step

        :type indx_word_src: string or None
        :param indx_word_src: similar to indx_word (but for the source
            language

        :type character_level: bool
        :param character_level: flag used when sampling, saying if we are
            running the model on characters or words

        :type excluding_params_for_norm: None or list of theano variables
        :param excluding_params_for_norm: list of parameters that should not
            be included when we compute the norm of the gradient (for norm
            clipping). Usually the output weights if the output layer is
            large

        :type rng: numpy random generator
        :param rng: numpy random generator

        """
        super(LM_Model, self).__init__(output_layer=cost_layer,
                                       sample_fn=sample_fn,
                                       indx_word=indx_word,
                                       indx_word_src=indx_word_src,
                                       rng=rng)
        if exclude_params_for_norm is None:
            self.exclude_params_for_norm = []
        else:
            self.exclude_params_for_norm = exclude_params_for_norm
        self.need_inputs_for_generating_noise=need_inputs_for_generating_noise
        self.cost_layer = cost_layer
        self.validate_step = valid_fn
        self.clean_noise_validation = clean_noise_validation
        self.noise_fn = noise_fn
        self.clean_before = clean_before_noise_fn
        self.weight_noise_amount = weight_noise_amount
        self.character_level = character_level

        self.valid_costs = ['cost','ppl']
        # Assume a single cost
        # We need to merge these lists
        state_below = self.cost_layer.state_below
        if hasattr(self.cost_layer, 'mask') and self.cost_layer.mask:
            num_words = TT.sum(self.cost_layer.mask)
        else:
            num_words = TT.cast(state_below.shape[0], 'float32')
        scale = getattr(self.cost_layer, 'cost_scale', numpy.float32(1))
        if not scale:
            scale = numpy.float32(1)
        scale *= numpy.float32(numpy.log(2))

        grad_norm = TT.sqrt(sum(TT.sum(x**2)
            for x,p in zip(self.param_grads, self.params) if p not in
                self.exclude_params_for_norm))
        new_properties = [
                ('grad_norm', grad_norm),
                ('log2_p_word', self.train_cost / num_words / scale),
                ('log2_p_expl', self.cost_layer.cost_per_sample.mean() / scale)]
        self.properties += new_properties

        if len(self.noise_params) >0 and weight_noise_amount:
            if self.need_inputs_for_generating_noise:
                inps = self.inputs
            else:
                inps = []
            self.add_noise = theano.function(inps,[],
                                             name='add_noise',
                                             updates = [(p,
                                                 self.trng.normal(shp_fn(self.inputs),
                                                     avg =0,
                                                     std=weight_noise_amount,
                                                     dtype=p.dtype))
                                                 for p, shp_fn in
                                                        zip(self.noise_params,
                                                         self.noise_params_shape_fn)],
                                            on_unused_input='ignore')
            self.del_noise = theano.function(inps,[],
                                             name='del_noise',
                                             updates=[(p,
                                                       TT.zeros(shp_fn(self.inputs),
                                                                p.dtype))
                                                      for p, shp_fn in
                                                      zip(self.noise_params,
                                                          self.noise_params_shape_fn)],
                                            on_unused_input='ignore')
        else:
            self.add_noise = None
            self.del_noise = None

Example 8

Project: NMT-Coverage
Source File: LM_model.py
View license
    def  __init__(self,
                  cost_layer = None,
                  sample_fn = None,
                  valid_fn = None,
                  noise_fn = None,
                  clean_before_noise_fn = False,
                  clean_noise_validation=True,
                  weight_noise_amount = 0,
                  indx_word="/data/lisa/data/PennTreebankCorpus/dictionaries.npz",
                  need_inputs_for_generating_noise=False,
                  indx_word_src=None,
                  character_level = False,
                  exclude_params_for_norm=None,
                  rng = None):
        """
        Constructs a model, that respects the interface required by the
        trainer class.

        :type cost_layer: groundhog layer
        :param cost_layer: the cost (last) layer of the model

        :type sample_fn: function or None
        :param sample_fn: function used to sample from the model

        :type valid_fn: function or None
        :param valid_fn: function used to compute the validation error on a
            minibatch of examples

        :type noise_fn: function or None
        :param noise_fn: function called to corrupt an input (that
            potentially will be denoised by the model)

        :type clean_before_noise_fn: bool
        :param clean_before_noise_fn: If the weight noise should be removed
            before calling the `noise_fn` to corrupt some input

        :type clean_noise_validation: bool
        :param clean_noise_validation: If the weight noise should be removed
            before calling the validation function

        :type weight_noise_amount: float or theano scalar
        :param weight_noise_amount: weight noise scale (standard deviation
            of the Gaussian from which it is sampled)

        :type indx_word: string or None
        :param indx_word: path to the file describing how to match indices
            to words (or characters)

        :type need_inputs_for_generating_noise: bool
        :param need_inputs_for_generating_noise: flag saying if the shape of
            the inputs affect the shape of the weight noise that is generated at
            each step

        :type indx_word_src: string or None
        :param indx_word_src: similar to indx_word (but for the source
            language

        :type character_level: bool
        :param character_level: flag used when sampling, saying if we are
            running the model on characters or words

        :type excluding_params_for_norm: None or list of theano variables
        :param excluding_params_for_norm: list of parameters that should not
            be included when we compute the norm of the gradient (for norm
            clipping). Usually the output weights if the output layer is
            large

        :type rng: numpy random generator
        :param rng: numpy random generator

        """
        super(LM_Model, self).__init__(output_layer=cost_layer,
                                       sample_fn=sample_fn,
                                       indx_word=indx_word,
                                       indx_word_src=indx_word_src,
                                       rng=rng)
        if exclude_params_for_norm is None:
            self.exclude_params_for_norm = []
        else:
            self.exclude_params_for_norm = exclude_params_for_norm
        self.need_inputs_for_generating_noise=need_inputs_for_generating_noise
        self.cost_layer = cost_layer
        self.validate_step = valid_fn
        self.clean_noise_validation = clean_noise_validation
        self.noise_fn = noise_fn
        self.clean_before = clean_before_noise_fn
        self.weight_noise_amount = weight_noise_amount
        self.character_level = character_level

        self.valid_costs = ['cost','ppl']
        # Assume a single cost
        # We need to merge these lists
        state_below = self.cost_layer.state_below
        if hasattr(self.cost_layer, 'mask') and self.cost_layer.mask:
            num_words = TT.sum(self.cost_layer.mask)
        else:
            num_words = TT.cast(state_below.shape[0], 'float32')
        scale = getattr(self.cost_layer, 'cost_scale', numpy.float32(1))
        if not scale:
            scale = numpy.float32(1)
        scale *= numpy.float32(numpy.log(2))

        grad_norm = TT.sqrt(sum(TT.sum(x**2)
            for x,p in zip(self.param_grads, self.params) if p not in
                self.exclude_params_for_norm))
        new_properties = [
                ('grad_norm', grad_norm),
                ('log2_p_word', self.train_cost / num_words / scale),
                ('log2_p_expl', self.cost_layer.cost_per_sample.mean() / scale)]
        self.properties += new_properties

        if len(self.noise_params) >0 and weight_noise_amount:
            if self.need_inputs_for_generating_noise:
                inps = self.inputs
            else:
                inps = []
            self.add_noise = theano.function(inps,[],
                                             name='add_noise',
                                             updates = [(p,
                                                 self.trng.normal(shp_fn(self.inputs),
                                                     avg =0,
                                                     std=weight_noise_amount,
                                                     dtype=p.dtype))
                                                 for p, shp_fn in
                                                        zip(self.noise_params,
                                                         self.noise_params_shape_fn)],
                                            on_unused_input='ignore')
            self.del_noise = theano.function(inps,[],
                                             name='del_noise',
                                             updates=[(p,
                                                       TT.zeros(shp_fn(self.inputs),
                                                                p.dtype))
                                                      for p, shp_fn in
                                                      zip(self.noise_params,
                                                          self.noise_params_shape_fn)],
                                            on_unused_input='ignore')
        else:
            self.add_noise = None
            self.del_noise = None

Example 9

Project: nupic.vision
Source File: __init__.py
View license
def blur(image, radius, sigma=None, edgeColor=None):
  """
  Gaussian blur with variable-sized kernel.

  Uses the fastest available method out of these:
  - OpenCV blur (only available on OS X and Windows).
  - C++ separable convolution (only available in the binary release).
  - Python separable convolution (always available).

  image -- Image (or numpy array).
  radius -- Kernel radius.
  sigma -- Standard deviation of the Gaussian, or None calculate a reasonable
    value from the radius.
  edgeColor -- Color with which to pad the edges for blurring. If not
    specified, the edge of the returned image is not blurred.

  Returns the blurred image (or if a numpy array was passed in, returns an
    array).
  """

  if type(radius) is not int:
    raise TypeError("'radius' must be an integer")

  if sigma is None:
    sigma = radius / 3.0
  elif sigma == 0:
    return
  length = radius * 2 - 1

  if isinstance(image, Image.Image):
    if image.mode == 'LA':
      blurredImage = blur(image.split()[0], radius, sigma, edgeColor)
      blurredAlpha = blur(image.split()[1], radius, sigma, 0)
      blurredImage.putalpha(blurredAlpha)
      return blurredImage
    elif image.mode != 'L':
      raise ValueError("Image must be mode 'L' or 'LA'")
    # Convert image to numpy array
    originalType = 'image'
    array = numpy.array(image).astype(numpy.float32)
  else:
    originalType = image.dtype
    array = image.astype(numpy.float32)

  if edgeColor is not None:
    # Pad image with edge color
    shape = [(d + (radius - 1) * 2) for d in array.shape]
    if edgeColor == 0:
      paddedArray = numpy.zeros(shape, numpy.float32)
    else:
      paddedArray = numpy.ones(shape, numpy.float32) * edgeColor
    paddedArray[radius-1:-(radius-1), radius-1:-(radius-1)] = array
    array = paddedArray

  # try:
  #   # Use OpenCV if available
  #   from opencv.cvtypes import cv
  #   method = 'opencv'
  # except ImportError:
  try:
    # Fall back on C++ convolution if available
    from nupic.bindings.algorithms import Float32SeparableConvolution2D
    method = 'cpp'
  except ImportError:
    # Use Python
    method = 'python'

  if method == 'opencv':
    # OpenCV method
    result = array.copy()
    header = cv.CreateImageHeader(cv.Size(result.shape[1], result.shape[0]),
                                  32, 1)
    header.contents.imageData = result.ctypes.data
    cv.Smooth(header1, header, cv.GAUSSIAN, length, length, sigma)
  else:
    # C++ or Python convolution: build kernel manually
    if sigma == numpy.inf:
      kernel = numpy.ones(length, numpy.float32)
    else:
      kernel = numpy.zeros(length, numpy.float32)
      for x in xrange(radius):
        val = 1 / (sigma * 2 * math.pi)
        val *= math.exp(-1 * x**2 / (2 * sigma**2))
        kernel[radius - 1 + x] = val
        kernel[radius - 1 - x] = val
    kernel /= kernel.sum()

    if method == 'cpp':
      # C++ method
      convolution = Float32SeparableConvolution2D()
      convolution.init(array.shape[0], array.shape[1],
                       kernel.shape[0], kernel.shape[0],
                       kernel, kernel)
      result = numpy.zeros(array.shape, numpy.float32)
      convolution.compute(array, result)

    elif method == 'python':
      # Python method
      # Rows
      padded1 = numpy.zeros((array.shape[0], array.shape[1] + radius - 1))
      padded1[:,:array.shape[1]] = array
      padded2 = numpy.convolve(padded1.flatten(), kernel, mode='same')
      padded2.resize(padded1.shape)
      array2 = padded2[:,:array.shape[1]]
      # Columns
      padded3 = numpy.zeros((array2.shape[0] + radius - 1, array2.shape[1]))
      padded3[:array2.shape[0],:] = array2
      padded3 = padded3.transpose()
      padded4 = numpy.convolve(padded3.flatten(), kernel, mode='same')
      padded4.resize(padded3.shape)
      padded4 = padded4.transpose()
      result = padded4[:array.shape[0],:]

  if edgeColor is None:
    # Paste the blurred image into the original image, cutting off the edge
    # of the blurred image, to avoid edge effects
    array[radius-1:-(radius-1), radius-1:-(radius-1)] = \
      result[radius-1:-(radius-1), radius-1:-(radius-1)]
    result = array
  else:
    # Crop out the padding
    result = result[radius-1:-(radius-1), radius-1:-(radius-1)]

  if originalType == 'image':
    return Image.fromarray(result.astype(numpy.uint8))
  else:
    return result.astype(originalType)

Example 10

Project: nupic.vision
Source File: __init__.py
View license
def blur(image, radius, sigma=None, edgeColor=None):
  """
  Gaussian blur with variable-sized kernel.

  Uses the fastest available method out of these:
  - OpenCV blur (only available on OS X and Windows).
  - C++ separable convolution (only available in the binary release).
  - Python separable convolution (always available).

  image -- Image (or numpy array).
  radius -- Kernel radius.
  sigma -- Standard deviation of the Gaussian, or None calculate a reasonable
    value from the radius.
  edgeColor -- Color with which to pad the edges for blurring. If not
    specified, the edge of the returned image is not blurred.

  Returns the blurred image (or if a numpy array was passed in, returns an
    array).
  """

  if type(radius) is not int:
    raise TypeError("'radius' must be an integer")

  if sigma is None:
    sigma = radius / 3.0
  elif sigma == 0:
    return
  length = radius * 2 - 1

  if isinstance(image, Image.Image):
    if image.mode == 'LA':
      blurredImage = blur(image.split()[0], radius, sigma, edgeColor)
      blurredAlpha = blur(image.split()[1], radius, sigma, 0)
      blurredImage.putalpha(blurredAlpha)
      return blurredImage
    elif image.mode != 'L':
      raise ValueError("Image must be mode 'L' or 'LA'")
    # Convert image to numpy array
    originalType = 'image'
    array = numpy.array(image).astype(numpy.float32)
  else:
    originalType = image.dtype
    array = image.astype(numpy.float32)

  if edgeColor is not None:
    # Pad image with edge color
    shape = [(d + (radius - 1) * 2) for d in array.shape]
    if edgeColor == 0:
      paddedArray = numpy.zeros(shape, numpy.float32)
    else:
      paddedArray = numpy.ones(shape, numpy.float32) * edgeColor
    paddedArray[radius-1:-(radius-1), radius-1:-(radius-1)] = array
    array = paddedArray

  # try:
  #   # Use OpenCV if available
  #   from opencv.cvtypes import cv
  #   method = 'opencv'
  # except ImportError:
  try:
    # Fall back on C++ convolution if available
    from nupic.bindings.algorithms import Float32SeparableConvolution2D
    method = 'cpp'
  except ImportError:
    # Use Python
    method = 'python'

  if method == 'opencv':
    # OpenCV method
    result = array.copy()
    header = cv.CreateImageHeader(cv.Size(result.shape[1], result.shape[0]),
                                  32, 1)
    header.contents.imageData = result.ctypes.data
    cv.Smooth(header1, header, cv.GAUSSIAN, length, length, sigma)
  else:
    # C++ or Python convolution: build kernel manually
    if sigma == numpy.inf:
      kernel = numpy.ones(length, numpy.float32)
    else:
      kernel = numpy.zeros(length, numpy.float32)
      for x in xrange(radius):
        val = 1 / (sigma * 2 * math.pi)
        val *= math.exp(-1 * x**2 / (2 * sigma**2))
        kernel[radius - 1 + x] = val
        kernel[radius - 1 - x] = val
    kernel /= kernel.sum()

    if method == 'cpp':
      # C++ method
      convolution = Float32SeparableConvolution2D()
      convolution.init(array.shape[0], array.shape[1],
                       kernel.shape[0], kernel.shape[0],
                       kernel, kernel)
      result = numpy.zeros(array.shape, numpy.float32)
      convolution.compute(array, result)

    elif method == 'python':
      # Python method
      # Rows
      padded1 = numpy.zeros((array.shape[0], array.shape[1] + radius - 1))
      padded1[:,:array.shape[1]] = array
      padded2 = numpy.convolve(padded1.flatten(), kernel, mode='same')
      padded2.resize(padded1.shape)
      array2 = padded2[:,:array.shape[1]]
      # Columns
      padded3 = numpy.zeros((array2.shape[0] + radius - 1, array2.shape[1]))
      padded3[:array2.shape[0],:] = array2
      padded3 = padded3.transpose()
      padded4 = numpy.convolve(padded3.flatten(), kernel, mode='same')
      padded4.resize(padded3.shape)
      padded4 = padded4.transpose()
      result = padded4[:array.shape[0],:]

  if edgeColor is None:
    # Paste the blurred image into the original image, cutting off the edge
    # of the blurred image, to avoid edge effects
    array[radius-1:-(radius-1), radius-1:-(radius-1)] = \
      result[radius-1:-(radius-1), radius-1:-(radius-1)]
    result = array
  else:
    # Crop out the padding
    result = result[radius-1:-(radius-1), radius-1:-(radius-1)]

  if originalType == 'image':
    return Image.fromarray(result.astype(numpy.uint8))
  else:
    return result.astype(originalType)

Example 11

Project: westpa
Source File: wcadimer.py
View license
def WCADimer(N=natoms, density=density, mm=None, mass=mass, epsilon=epsilon, sigma=sigma, h=h, r0=r0, w=w):
    """
    Create a bistable bonded pair of particles (indices 0 and 1) optionally surrounded by a Weeks-Chandler-Andersen fluid.

    The bistable potential has form

    U(r) = h*(1-((r-r0-w)/w)^2)^2

    where r0 is the compact state separation, r0+2w is the extended state separation, and h is the barrier height.

    The WCA potential has form

    U(r) = 4 epsilon [ (sigma/r)^12 - (sigma/r)^6 ] + epsilon      (r < r*)
         = 0                                                       (r >= r*)

    where r* = 2^(1/6) sigma.

    OPTIONAL ARGUMENTS

    N (int) - total number of atoms (default: 2)
    density (float) - number density of particles (default: 0.96 / sigma**3)
    mass (simtk.unit.Quantity of mass) - particle mass (default: 39.948 amu)
    sigma (simtk.unit.Quantity of length) - Lennard-Jones sigma parameter (default: 0.3405 nm)
    epsilon (simtk.unit.Quantity of energy) - Lennard-Jones well depth (default: (119.8 Kelvin)*kB)
    h (simtk.unit.Quantity of energy) - bistable potential barrier height (default: ???)
    r0 (simtk.unit.Quantity of length) - bistable potential compact state separation (default: ???)
    w (simtk.unit.Quantity of length) - bistable potential extended state separation is r0+2*w (default: ???)

    """
    
    # Choose OpenMM package.
    if mm is None:
        mm = openmm

    # Compute cutoff for WCA fluid.
    r_WCA = 2.**(1./6.) * sigma # cutoff at minimum of potential

    # Create system
    system = mm.System()

    # Compute total system volume.
    volume = N / density
    
    # Make system cubic in dimension.
    length = volume**(1.0/3.0)
    a = units.Quantity(numpy.array([1.0, 0.0, 0.0], numpy.float32), units.nanometer) * length/units.nanometer
    b = units.Quantity(numpy.array([0.0, 1.0, 0.0], numpy.float32), units.nanometer) * length/units.nanometer
    c = units.Quantity(numpy.array([0.0, 0.0, 1.0], numpy.float32), units.nanometer) * length/units.nanometer
    print "box edge length = %s" % str(length)
    system.setDefaultPeriodicBoxVectors(a, b, c)

    # Add particles to system.
    for n in range(N):
        system.addParticle(mass)
            
    # WCA: Lennard-Jones truncated at minim and shifted so potential is zero at cutoff.
    energy_expression = '4.0*epsilon*((sigma/r)^12 - (sigma/r)^6) + epsilon'
    
    # Create force.
    force = mm.CustomNonbondedForce(energy_expression)

    # Set epsilon and sigma global parameters.
    force.addGlobalParameter('epsilon', epsilon)
    force.addGlobalParameter('sigma', sigma)

    # Add particles
    for n in range(N):
        force.addParticle([])

    # Add exclusion between bonded particles.
    force.addExclusion(0,1)
    
    # Set periodic boundary conditions with cutoff.
    if (N > 2):
        force.setNonbondedMethod(mm.CustomNonbondedForce.CutoffPeriodic)
    else:
        force.setNonbondedMethod(mm.CustomNonbondedForce.CutoffNonPeriodic)
    print "setting cutoff distance to %s" % str(r_WCA)
    force.setCutoffDistance(r_WCA)    

    # Add nonbonded force term to the system.
    system.addForce(force)

    # Add dimer potential to first two particles.
    dimer_force = openmm.CustomBondForce('h*(1-((r-r0-w)/w)^2)^2;')
    dimer_force.addGlobalParameter('h', h) # barrier height
    dimer_force.addGlobalParameter('r0', r0) # compact state separation
    dimer_force.addGlobalParameter('w', w) # second minimum is at r0 + 2*w
    dimer_force.addBond(0, 1, [])
    system.addForce(dimer_force)

    # Create initial coordinates using random positions.
    coordinates = units.Quantity(numpy.random.rand(N,3), units.nanometer) * (length / units.nanometer)
       
    # Reposition dimer particles at compact minimum.
    coordinates[0,:] *= 0.0
    coordinates[1,:] *= 0.0
    coordinates[1,0] = r0

    # Return system and coordinates.
    return [system, coordinates]

Example 12

Project: Cura
Source File: ImageReader.py
View license
    def _generateSceneNode(self, file_name, xz_size, peak_height, base_height, blur_iterations, max_size, image_color_invert):
        scene_node = SceneNode()

        mesh = MeshBuilder()

        img = QImage(file_name)

        if img.isNull():
            Logger.log("e", "Image is corrupt.")
            return None

        width = max(img.width(), 2)
        height = max(img.height(), 2)
        aspect = height / width

        if img.width() < 2 or img.height() < 2:
            img = img.scaled(width, height, Qt.IgnoreAspectRatio)

        base_height = max(base_height, 0)
        peak_height = max(peak_height, -base_height)

        xz_size = max(xz_size, 1)
        scale_vector = Vector(xz_size, peak_height, xz_size)

        if width > height:
            scale_vector = scale_vector.set(z=scale_vector.z * aspect)
        elif height > width:
            scale_vector = scale_vector.set(x=scale_vector.x / aspect)

        if width > max_size or height > max_size:
            scale_factor = max_size / width
            if height > width:
                scale_factor = max_size / height

            width = int(max(round(width * scale_factor), 2))
            height = int(max(round(height * scale_factor), 2))
            img = img.scaled(width, height, Qt.IgnoreAspectRatio)

        width_minus_one = width - 1
        height_minus_one = height - 1

        Job.yieldThread()

        texel_width = 1.0 / (width_minus_one) * scale_vector.x
        texel_height = 1.0 / (height_minus_one) * scale_vector.z

        height_data = numpy.zeros((height, width), dtype=numpy.float32)

        for x in range(0, width):
            for y in range(0, height):
                qrgb = img.pixel(x, y)
                avg = float(qRed(qrgb) + qGreen(qrgb) + qBlue(qrgb)) / (3 * 255)
                height_data[y, x] = avg

        Job.yieldThread()

        if image_color_invert:
            height_data = 1 - height_data

        for _ in range(0, blur_iterations):
            copy = numpy.pad(height_data, ((1, 1), (1, 1)), mode= "edge")

            height_data += copy[1:-1, 2:]
            height_data += copy[1:-1, :-2]
            height_data += copy[2:, 1:-1]
            height_data += copy[:-2, 1:-1]

            height_data += copy[2:, 2:]
            height_data += copy[:-2, 2:]
            height_data += copy[2:, :-2]
            height_data += copy[:-2, :-2]

            height_data /= 9

            Job.yieldThread()

        height_data *= scale_vector.y
        height_data += base_height

        heightmap_face_count = 2 * height_minus_one * width_minus_one
        total_face_count = heightmap_face_count + (width_minus_one * 2) * (height_minus_one * 2) + 2

        mesh.reserveFaceCount(total_face_count)

        # initialize to texel space vertex offsets.
        # 6 is for 6 vertices for each texel quad.
        heightmap_vertices = numpy.zeros((width_minus_one * height_minus_one, 6, 3), dtype = numpy.float32)
        heightmap_vertices = heightmap_vertices + numpy.array([[
            [0, base_height, 0],
            [0, base_height, texel_height],
            [texel_width, base_height, texel_height],
            [texel_width, base_height, texel_height],
            [texel_width, base_height, 0],
            [0, base_height, 0]
        ]], dtype = numpy.float32)

        offsetsz, offsetsx = numpy.mgrid[0: height_minus_one, 0: width - 1]
        offsetsx = numpy.array(offsetsx, numpy.float32).reshape(-1, 1) * texel_width
        offsetsz = numpy.array(offsetsz, numpy.float32).reshape(-1, 1) * texel_height

        # offsets for each texel quad
        heightmap_vertex_offsets = numpy.concatenate([offsetsx, numpy.zeros((offsetsx.shape[0], offsetsx.shape[1]), dtype=numpy.float32), offsetsz], 1)
        heightmap_vertices += heightmap_vertex_offsets.repeat(6, 0).reshape(-1, 6, 3)

        # apply height data to y values
        heightmap_vertices[:, 0, 1] = heightmap_vertices[:, 5, 1] = height_data[:-1, :-1].reshape(-1)
        heightmap_vertices[:, 1, 1] = height_data[1:, :-1].reshape(-1)
        heightmap_vertices[:, 2, 1] = heightmap_vertices[:, 3, 1] = height_data[1:, 1:].reshape(-1)
        heightmap_vertices[:, 4, 1] = height_data[:-1, 1:].reshape(-1)

        heightmap_indices = numpy.array(numpy.mgrid[0:heightmap_face_count * 3], dtype=numpy.int32).reshape(-1, 3)

        mesh._vertices[0:(heightmap_vertices.size // 3), :] = heightmap_vertices.reshape(-1, 3)
        mesh._indices[0:(heightmap_indices.size // 3), :] = heightmap_indices

        mesh._vertex_count = heightmap_vertices.size // 3
        mesh._face_count = heightmap_indices.size // 3

        geo_width = width_minus_one * texel_width
        geo_height = height_minus_one * texel_height

        # bottom
        mesh.addFaceByPoints(0, 0, 0, 0, 0, geo_height, geo_width, 0, geo_height)
        mesh.addFaceByPoints(geo_width, 0, geo_height, geo_width, 0, 0, 0, 0, 0)

        # north and south walls
        for n in range(0, width_minus_one):
            x = n * texel_width
            nx = (n + 1) * texel_width

            hn0 = height_data[0, n]
            hn1 = height_data[0, n + 1]

            hs0 = height_data[height_minus_one, n]
            hs1 = height_data[height_minus_one, n + 1]

            mesh.addFaceByPoints(x, 0, 0, nx, 0, 0, nx, hn1, 0)
            mesh.addFaceByPoints(nx, hn1, 0, x, hn0, 0, x, 0, 0)

            mesh.addFaceByPoints(x, 0, geo_height, nx, 0, geo_height, nx, hs1, geo_height)
            mesh.addFaceByPoints(nx, hs1, geo_height, x, hs0, geo_height, x, 0, geo_height)

        # west and east walls
        for n in range(0, height_minus_one):
            y = n * texel_height
            ny = (n + 1) * texel_height

            hw0 = height_data[n, 0]
            hw1 = height_data[n + 1, 0]

            he0 = height_data[n, width_minus_one]
            he1 = height_data[n + 1, width_minus_one]

            mesh.addFaceByPoints(0, 0, y, 0, 0, ny, 0, hw1, ny)
            mesh.addFaceByPoints(0, hw1, ny, 0, hw0, y, 0, 0, y)

            mesh.addFaceByPoints(geo_width, 0, y, geo_width, 0, ny, geo_width, he1, ny)
            mesh.addFaceByPoints(geo_width, he1, ny, geo_width, he0, y, geo_width, 0, y)

        mesh.calculateNormals(fast=True)

        scene_node.setMeshData(mesh.build())

        return scene_node

Example 13

Project: gpgpu-neuralnet
Source File: training.py
View license
    def start_training( self, context, training_data, training_results,
                        maximal_iterations = 10000, target_error = 0.01,
                        report = False ):
        """
        Starts training.
        
        @param context
            Input layer of neural network.
            
        @param training_data
            Array of tuples of inputs and outputs.
            
        @param training_results
            TrainingResults structure where optimal results will be stored.
            
        @param maximal_iterations
            Maximal iteration to perform.
            
        @param target_error
            Target absolute error.
            
        @param report
            Report object (optimal)
            
        @return Tuple of performed iterations count, minimal relative error
        """

        start_time = time.clock()

        self.prepare_training( context )

        total_error = numpy.array( [1e12], numpy.float32 )
        total_error_buf = pyopencl.Buffer( 
            context.opencl.context, pyopencl.mem_flags.READ_WRITE | pyopencl.mem_flags.COPY_HOST_PTR,
            hostbuf = total_error )

        zeros_buf = pyopencl.Buffer( 
            context.opencl.context,
            pyopencl.mem_flags.READ_ONLY | pyopencl.mem_flags.COPY_HOST_PTR,
            hostbuf = numpy.zeros( [context._weights_buf_size], numpy.float32 )
            )

        read_ready_event = None

        o_buf = pyopencl.Buffer( 
            context.opencl.context, pyopencl.mem_flags.READ_ONLY | pyopencl.mem_flags.COPY_HOST_PTR,
            hostbuf = numpy.zeros( [context.output_layer.neuron_count], numpy.float32 )
            )

        context.opencl.kernel_setup_training_data.set_arg( 0, context._neurons_buf_size )
        context.opencl.kernel_setup_training_data.set_arg( 1, context._outputs_buf )
        context.opencl.kernel_setup_training_data.set_arg( 2, context.output_layer._neurons_offset )
        context.opencl.kernel_setup_training_data.set_arg( 3, context.output_layer.neuron_count )
        context.opencl.kernel_setup_training_data.set_arg( 4, o_buf )
        context.opencl.kernel_setup_training_data.set_arg( 5, pyopencl.LocalMemory( 32 * 4 ) )
        context.opencl.kernel_setup_training_data.set_arg( 6, context._errors_backpropagation_buf )
        context.opencl.kernel_setup_training_data.set_arg( 7, total_error_buf )

        # clear gradient
        pyopencl.enqueue_copy_buffer( 
            context.opencl.queue, zeros_buf, context._gradient_buf
            ).wait()

        i = 0
        calc_error_evt = None
        while training_results.minimal_error > target_error:
            if i >= maximal_iterations:
                break
            i += 1

            reset_total_error_evt = pyopencl.enqueue_copy_buffer( context.opencl.queue, zeros_buf, total_error_buf, byte_count = 4 )
            j = 0
            for inputs, outputs in training_data:
                j += 1
#                pyopencl.enqueue_barrier( context.opencl.queue )
                evt = context.input_layer.set_inputs( inputs, is_blocking = False )
                context.input_layer._process_wait_for.append( evt )
                context.input_layer.process()

                evt = pyopencl.enqueue_write_buffer( 
                    context.opencl.queue, o_buf, outputs, is_blocking = False
                    )

                calc_error_evt = pyopencl.enqueue_nd_range_kernel( 
                    context.opencl.queue,
                    context.opencl.kernel_setup_training_data,
                    ( 32, ), ( 32, ),
                    wait_for = ( evt, context.output_layer._process_event, reset_total_error_evt )
                    )

#                print context.output_layer.get_outputs()

                context.output_layer._calc_gradient_wait_for.append( calc_error_evt )
                context.input_layer.calc_weights_gradient()
                #print context.output_layer._get_gradient( )

                if not self.offline:
                    self.adjust_weights( context )
                    evt = pyopencl.enqueue_copy_buffer( 
                        context.opencl.queue, zeros_buf, context._gradient_buf,
                        wait_for = ( context.input_layer._calc_gradient_event, )
                        )
                    context.output_layer._calc_gradient_wait_for.append( evt )

                if j % 20000 == 0:
                    context.opencl.queue.finish()

            if self.offline:
                save_n = self.n
                self.n /= numpy.float32( len( training_data ) )
                self.adjust_weights( context )
                self.n = save_n
                evt = pyopencl.enqueue_copy_buffer( context.opencl.queue, zeros_buf, context._gradient_buf )
                context.output_layer._calc_gradient_wait_for.append( evt )

            if read_ready_event and read_ready_event.command_execution_status == pyopencl.command_execution_status.COMPLETE:
                read_ready_event = None
                error_sum = total_error[0] / len( training_data )
#                print error_sum, ' ', i, ' ', self.n

                if report:
                    report.process_iteration( len( training_data ), self, training_results, error_sum, context )

                self.adjust_training_parameters( error_sum )

                if error_sum < training_results.minimal_error:
                    training_results.minimal_error = error_sum
                    training_results.store_weights( context )   # note: this call is blocking!

                if error_sum < target_error:
                    break;

                training_results.opencl_time += context.opencl.gather_opencl_stats()

            if not read_ready_event:
                # we use nonblocking read to avoid waiting for GPU
                # this could lead to a delay in obtaining current error
                # error of current iteration can be returned in several iteration ahead
                read_ready_event = pyopencl.enqueue_read_buffer( 
                    context.opencl.queue, total_error_buf,
                    total_error, is_blocking = False,
                    wait_for = ( calc_error_evt, ) if calc_error_evt else None
                    )

        training_results.iterations += i

        pyopencl.enqueue_read_buffer( 
            context.opencl.queue, total_error_buf,
            total_error, is_blocking = True,
            wait_for = ( calc_error_evt, ) if calc_error_evt else None
            )
        error_sum = total_error[0] / len( training_data )

        if error_sum < training_results.minimal_error:
            training_results.minimal_error = error_sum
            training_results.store_weights( context )

        training_results.opencl_time += context.opencl.gather_opencl_stats()
        training_results.total_time += time.clock() - start_time

Example 14

Project: hifive
Source File: hic_domains.py
View license
    def find_arrowhead_TADs(self, binsize, minbins, maxbins, chroms=[]):
        if isinstance(chroms, str):
            chroms = [chroms]
        if len(chroms) == 0:
            chroms = list(self.hic.fends['chromosomes'][...])
        self.binsize = int(binsize)
        self.minsize = self.binsize * max(1, int(minbins))
        self.maxsize = self.binsize * (int(maxbins) + 1)
        self.TADs = {}
        for chrom in chroms:
            self.TADs[chrom] = []
            if not self.silent:
                print >> sys.stderr, ("\r%s\rFinding heatmap for chromosome %s...") % (' ' * 80, chrom),
            temp = self.hic.cis_heatmap(chrom, binsize=binsize * 16, datatype='fend', arraytype='full', returnmapping=True)
            if temp is None:
                continue
            temp_data, mapping = temp
            heatmap = numpy.zeros((temp_data.shape[0] * 16, temp_data.shape[0] * 16, 2), dtype=numpy.float32)
            temp = numpy.zeros(heatmap.shape, dtype=numpy.float32)
            for i in range(16):
                for j in range(16):
                    heatmap[i::16, j::16, :] += temp_data
            temp_data = self.hic.cis_heatmap(chrom, binsize=binsize * 4, datatype='fend', arraytype='full', start=mapping[0, 0], stop=mapping[-1, 1])
            for i in range(4):
                for j in range(4):
                    temp[i::4, j::4, :] += temp_data
            where = numpy.where(temp[:, :, 0] > 0)
            heatmap[where[0], where[1], :] = temp[where[0], where[1], :]
            temp_data, mapping = self.hic.cis_heatmap(chrom, binsize=binsize, datatype='fend', arraytype='full', start=mapping[0, 0], stop=mapping[-1, 1], returnmapping=True)
            where = numpy.where(temp_data[:, :, 0] > 0)
            heatmap[where[0], where[1], :] = temp_data[where[0], where[1], :]
            data = numpy.zeros((heatmap.shape[0], maxbins - 1, 2), dtype=numpy.float32)
            for i in range(heatmap.shape[0] - 1):
                data[i, :min(data.shape[1], data.shape[0] - i - 1), :] = heatmap[i, (i + 1):min(data.shape[1] + i + 1, data.shape[0]), :]
            where = numpy.where(data[:, :, 1] > 0)
            data[where[0], where[1], 0] /= data[where[0], where[1], 1]
            scores = numpy.zeros((data.shape[0], data.shape[1]), dtype=numpy.float32)
            if not self.silent:
                print >> sys.stderr, ("\r%s\rFinding arrowhead transformation for chromosome %s...") % (' ' * 80, chrom),
            _hic_domains.find_arrowhead_transformation(data, scores, maxbins)
            sums = numpy.zeros(data.shape, dtype=numpy.float32)
            signs = numpy.zeros(data.shape, dtype=numpy.float32)
            variances = numpy.zeros((data.shape[0], data.shape[1], 2, 2), dtype=numpy.float32)
            domain_scores = numpy.zeros(scores.shape, dtype=numpy.float32)
            if not self.silent:
                print >> sys.stderr, ("\r%s\rFinding arrowhead scoring for chromosome %s...") % (' ' * 80, chrom),
            _hic_domains.find_arrowhead_scores(scores, sums, signs, variances, domain_scores, minbins)
            path = numpy.zeros(data.shape[0], dtype=numpy.int32)
            path_scores = numpy.zeros(data.shape[0], dtype=numpy.float64)
            if not self.silent:
                print >> sys.stderr, ("\r%s\rFinding optimal domains for chromosome %s...") % (' ' * 80, chrom),
            _hic_domains.find_arrowhead_path(domain_scores, path, path_scores, minbins, maxbins)
            i = path.shape[0] - 1
            domains = []
            while i > 0:
                if path[i] != 1:
                    domains.append([i - path[i], i])
                    self.TADs[chrom].append([mapping[domains[-1][0], 0], mapping[domains[-1][1], 1]])
                i -= path[i]
        if not self.silent:
            print >> sys.stderr, ("\r%s\rFinished finding TADs\n") % (' ' * 80),

Example 15

Project: westpa
Source File: wcadimer.py
View license
def WCADimer(N=natoms, density=density, mm=None, mass=mass, epsilon=epsilon, sigma=sigma, h=h, r0=r0, w=w):
    """
    Create a bistable bonded pair of particles (indices 0 and 1) optionally surrounded by a Weeks-Chandler-Andersen fluid.

    The bistable potential has form

    U(r) = h*(1-((r-r0-w)/w)^2)^2

    where r0 is the compact state separation, r0+2w is the extended state separation, and h is the barrier height.

    The WCA potential has form

    U(r) = 4 epsilon [ (sigma/r)^12 - (sigma/r)^6 ] + epsilon      (r < r*)
         = 0                                                       (r >= r*)

    where r* = 2^(1/6) sigma.

    OPTIONAL ARGUMENTS

    N (int) - total number of atoms (default: 2)
    density (float) - number density of particles (default: 0.96 / sigma**3)
    mass (simtk.unit.Quantity of mass) - particle mass (default: 39.948 amu)
    sigma (simtk.unit.Quantity of length) - Lennard-Jones sigma parameter (default: 0.3405 nm)
    epsilon (simtk.unit.Quantity of energy) - Lennard-Jones well depth (default: (119.8 Kelvin)*kB)
    h (simtk.unit.Quantity of energy) - bistable potential barrier height (default: ???)
    r0 (simtk.unit.Quantity of length) - bistable potential compact state separation (default: ???)
    w (simtk.unit.Quantity of length) - bistable potential extended state separation is r0+2*w (default: ???)

    """
    
    # Choose OpenMM package.
    if mm is None:
        mm = openmm

    # Compute cutoff for WCA fluid.
    r_WCA = 2.**(1./6.) * sigma # cutoff at minimum of potential

    # Create system
    system = mm.System()

    # Compute total system volume.
    volume = N / density
    
    # Make system cubic in dimension.
    length = volume**(1.0/3.0)
    a = units.Quantity(numpy.array([1.0, 0.0, 0.0], numpy.float32), units.nanometer) * length/units.nanometer
    b = units.Quantity(numpy.array([0.0, 1.0, 0.0], numpy.float32), units.nanometer) * length/units.nanometer
    c = units.Quantity(numpy.array([0.0, 0.0, 1.0], numpy.float32), units.nanometer) * length/units.nanometer
    print "box edge length = %s" % str(length)
    system.setDefaultPeriodicBoxVectors(a, b, c)

    # Add particles to system.
    for n in range(N):
        system.addParticle(mass)
            
    # WCA: Lennard-Jones truncated at minim and shifted so potential is zero at cutoff.
    energy_expression = '4.0*epsilon*((sigma/r)^12 - (sigma/r)^6) + epsilon'
    
    # Create force.
    force = mm.CustomNonbondedForce(energy_expression)

    # Set epsilon and sigma global parameters.
    force.addGlobalParameter('epsilon', epsilon)
    force.addGlobalParameter('sigma', sigma)

    # Add particles
    for n in range(N):
        force.addParticle([])

    # Add exclusion between bonded particles.
    force.addExclusion(0,1)
    
    # Set periodic boundary conditions with cutoff.
    if (N > 2):
        force.setNonbondedMethod(mm.CustomNonbondedForce.CutoffPeriodic)
    else:
        force.setNonbondedMethod(mm.CustomNonbondedForce.CutoffNonPeriodic)
    print "setting cutoff distance to %s" % str(r_WCA)
    force.setCutoffDistance(r_WCA)    

    # Add nonbonded force term to the system.
    system.addForce(force)

    # Add dimer potential to first two particles.
    dimer_force = openmm.CustomBondForce('h*(1-((r-r0-w)/w)^2)^2;')
    dimer_force.addGlobalParameter('h', h) # barrier height
    dimer_force.addGlobalParameter('r0', r0) # compact state separation
    dimer_force.addGlobalParameter('w', w) # second minimum is at r0 + 2*w
    dimer_force.addBond(0, 1, [])
    system.addForce(dimer_force)

    # Create initial coordinates using random positions.
    coordinates = units.Quantity(numpy.random.rand(N,3), units.nanometer) * (length / units.nanometer)
       
    # Reposition dimer particles at compact minimum.
    coordinates[0,:] *= 0.0
    coordinates[1,:] *= 0.0
    coordinates[1,0] = r0

    # Return system and coordinates.
    return [system, coordinates]

Example 16

Project: westpa
Source File: wcadimer.py
View license
def WCADimer(N=natoms, density=density, mm=None, mass=mass, epsilon=epsilon, sigma=sigma, h=h, r0=r0, w=w):
    """
    Create a bistable bonded pair of particles (indices 0 and 1) optionally surrounded by a Weeks-Chandler-Andersen fluid.

    The bistable potential has form

    U(r) = h*(1-((r-r0-w)/w)^2)^2

    where r0 is the compact state separation, r0+2w is the extended state separation, and h is the barrier height.

    The WCA potential has form

    U(r) = 4 epsilon [ (sigma/r)^12 - (sigma/r)^6 ] + epsilon      (r < r*)
         = 0                                                       (r >= r*)

    where r* = 2^(1/6) sigma.

    OPTIONAL ARGUMENTS

    N (int) - total number of atoms (default: 2)
    density (float) - number density of particles (default: 0.96 / sigma**3)
    mass (simtk.unit.Quantity of mass) - particle mass (default: 39.948 amu)
    sigma (simtk.unit.Quantity of length) - Lennard-Jones sigma parameter (default: 0.3405 nm)
    epsilon (simtk.unit.Quantity of energy) - Lennard-Jones well depth (default: (119.8 Kelvin)*kB)
    h (simtk.unit.Quantity of energy) - bistable potential barrier height (default: ???)
    r0 (simtk.unit.Quantity of length) - bistable potential compact state separation (default: ???)
    w (simtk.unit.Quantity of length) - bistable potential extended state separation is r0+2*w (default: ???)

    """
    
    # Choose OpenMM package.
    if mm is None:
        mm = openmm

    # Compute cutoff for WCA fluid.
    r_WCA = 2.**(1./6.) * sigma # cutoff at minimum of potential

    # Create system
    system = mm.System()

    # Compute total system volume.
    volume = N / density
    
    # Make system cubic in dimension.
    length = volume**(1.0/3.0)
    a = units.Quantity(numpy.array([1.0, 0.0, 0.0], numpy.float32), units.nanometer) * length/units.nanometer
    b = units.Quantity(numpy.array([0.0, 1.0, 0.0], numpy.float32), units.nanometer) * length/units.nanometer
    c = units.Quantity(numpy.array([0.0, 0.0, 1.0], numpy.float32), units.nanometer) * length/units.nanometer
    print "box edge length = %s" % str(length)
    system.setDefaultPeriodicBoxVectors(a, b, c)

    # Add particles to system.
    for n in range(N):
        system.addParticle(mass)
            
    # WCA: Lennard-Jones truncated at minim and shifted so potential is zero at cutoff.
    energy_expression = '4.0*epsilon*((sigma/r)^12 - (sigma/r)^6) + epsilon'
    
    # Create force.
    force = mm.CustomNonbondedForce(energy_expression)

    # Set epsilon and sigma global parameters.
    force.addGlobalParameter('epsilon', epsilon)
    force.addGlobalParameter('sigma', sigma)

    # Add particles
    for n in range(N):
        force.addParticle([])

    # Add exclusion between bonded particles.
    force.addExclusion(0,1)
    
    # Set periodic boundary conditions with cutoff.
    if (N > 2):
        force.setNonbondedMethod(mm.CustomNonbondedForce.CutoffPeriodic)
    else:
        force.setNonbondedMethod(mm.CustomNonbondedForce.CutoffNonPeriodic)
    print "setting cutoff distance to %s" % str(r_WCA)
    force.setCutoffDistance(r_WCA)    

    # Add nonbonded force term to the system.
    system.addForce(force)

    # Add dimer potential to first two particles.
    dimer_force = openmm.CustomBondForce('h*(1-((r-r0-w)/w)^2)^2;')
    dimer_force.addGlobalParameter('h', h) # barrier height
    dimer_force.addGlobalParameter('r0', r0) # compact state separation
    dimer_force.addGlobalParameter('w', w) # second minimum is at r0 + 2*w
    dimer_force.addBond(0, 1, [])
    system.addForce(dimer_force)

    # Create initial coordinates using random positions.
    coordinates = units.Quantity(numpy.random.rand(N,3), units.nanometer) * (length / units.nanometer)
       
    # Reposition dimer particles at compact minimum.
    coordinates[0,:] *= 0.0
    coordinates[1,:] *= 0.0
    coordinates[1,0] = r0

    # Return system and coordinates.
    return [system, coordinates]

Example 17

Project: Cura
Source File: BuildVolume.py
View license
    def _updateDisallowedAreas(self):
        if not self._global_container_stack:
            return

        self._has_errors = False  # Reset.
        self._error_areas = []
        disallowed_areas = copy.deepcopy(
            self._global_container_stack.getProperty("machine_disallowed_areas", "value"))
        areas = []

        machine_width = self._global_container_stack.getProperty("machine_width", "value")
        machine_depth = self._global_container_stack.getProperty("machine_depth", "value")
        prime_tower_area = None

        # Add prime tower location as disallowed area.
        if ExtruderManager.getInstance().getResolveOrValue("prime_tower_enable") == True:
            prime_tower_size = self._global_container_stack.getProperty("prime_tower_size", "value")
            prime_tower_x = self._global_container_stack.getProperty("prime_tower_position_x", "value") - machine_width / 2
            prime_tower_y = - self._global_container_stack.getProperty("prime_tower_position_y", "value") + machine_depth / 2

            prime_tower_area = Polygon([
                [prime_tower_x - prime_tower_size, prime_tower_y - prime_tower_size],
                [prime_tower_x, prime_tower_y - prime_tower_size],
                [prime_tower_x, prime_tower_y],
                [prime_tower_x - prime_tower_size, prime_tower_y],
            ])
        disallowed_polygons = []

        # Check if prime positions intersect with disallowed areas
        prime_collision = False
        if disallowed_areas:
            for area in disallowed_areas:
                poly = Polygon(numpy.array(area, numpy.float32))

                # Minkowski with zero, to ensure that the polygon is correct & watertight.
                poly = poly.getMinkowskiHull(Polygon.approximatedCircle(0))
                disallowed_polygons.append(poly)

            extruder_manager = ExtruderManager.getInstance()
            extruders = extruder_manager.getMachineExtruders(self._global_container_stack.getId())
            prime_polygons = []
            # Each extruder has it's own prime location
            for extruder in extruders:
                prime_x = extruder.getProperty("extruder_prime_pos_x", "value") - machine_width / 2
                prime_y = machine_depth / 2 - extruder.getProperty("extruder_prime_pos_y", "value")

                prime_polygon = Polygon([
                    [prime_x - PRIME_CLEARANCE, prime_y - PRIME_CLEARANCE],
                    [prime_x + PRIME_CLEARANCE, prime_y - PRIME_CLEARANCE],
                    [prime_x + PRIME_CLEARANCE, prime_y + PRIME_CLEARANCE],
                    [prime_x - PRIME_CLEARANCE, prime_y + PRIME_CLEARANCE],
                ])
                prime_polygon = prime_polygon.getMinkowskiHull(Polygon.approximatedCircle(0))
                collision = False

                # Check if prime polygon is intersecting with any of the other disallowed areas.
                # Note that we check the prime area without bed adhesion.
                for poly in disallowed_polygons:
                    if prime_polygon.intersectsPolygon(poly) is not None:
                        collision = True
                        break

                # Also collide with other prime positions
                for poly in prime_polygons:
                    if prime_polygon.intersectsPolygon(poly) is not None:
                        collision = True
                        break

                if not collision:
                    # Prime area is valid. Add as normal.
                    # Once it's added like this, it will recieve a bed adhesion offset, just like the others.
                    prime_polygons.append(prime_polygon)
                else:
                    self._error_areas.append(prime_polygon)
                    prime_collision = collision or prime_collision

            disallowed_polygons.extend(prime_polygons)

        disallowed_border_size = self._getEdgeDisallowedSize()

        # Extend every area already in the disallowed_areas with the skirt size.
        if disallowed_areas:
            for poly in disallowed_polygons:
                poly = poly.getMinkowskiHull(Polygon.approximatedCircle(disallowed_border_size))
                areas.append(poly)

        # Add the skirt areas around the borders of the build plate.
        if disallowed_border_size > 0:
            half_machine_width = self._global_container_stack.getProperty("machine_width", "value") / 2
            half_machine_depth = self._global_container_stack.getProperty("machine_depth", "value") / 2

            areas.append(Polygon(numpy.array([
                [-half_machine_width, -half_machine_depth],
                [-half_machine_width, half_machine_depth],
                [-half_machine_width + disallowed_border_size, half_machine_depth - disallowed_border_size],
                [-half_machine_width + disallowed_border_size, -half_machine_depth + disallowed_border_size]
            ], numpy.float32)))

            areas.append(Polygon(numpy.array([
                [half_machine_width, half_machine_depth],
                [half_machine_width, -half_machine_depth],
                [half_machine_width - disallowed_border_size, -half_machine_depth + disallowed_border_size],
                [half_machine_width - disallowed_border_size, half_machine_depth - disallowed_border_size]
            ], numpy.float32)))

            areas.append(Polygon(numpy.array([
                [-half_machine_width, half_machine_depth],
                [half_machine_width, half_machine_depth],
                [half_machine_width - disallowed_border_size, half_machine_depth - disallowed_border_size],
                [-half_machine_width + disallowed_border_size, half_machine_depth - disallowed_border_size]
            ], numpy.float32)))

            areas.append(Polygon(numpy.array([
                [half_machine_width, -half_machine_depth],
                [-half_machine_width, -half_machine_depth],
                [-half_machine_width + disallowed_border_size, -half_machine_depth + disallowed_border_size],
                [half_machine_width - disallowed_border_size, -half_machine_depth + disallowed_border_size]
            ], numpy.float32)))

        # Check if the prime tower area intersects with any of the other areas.
        # If this is the case, add it to the error area's so it can be drawn in red.
        # If not, add it back to disallowed area's, so it's rendered as normal.
        prime_tower_collision = False
        if prime_tower_area:
            # Using Minkowski of 0 fixes the prime tower area so it's rendered correctly
            prime_tower_area = prime_tower_area.getMinkowskiHull(Polygon.approximatedCircle(0))
            for area in areas:
                if prime_tower_area.intersectsPolygon(area) is not None:
                    prime_tower_collision = True
                    break

            if not prime_tower_collision:
                areas.append(prime_tower_area)
            else:
                self._error_areas.append(prime_tower_area)
        # The buildplate has errors if either prime tower or prime has a colission.
        self._has_errors = prime_tower_collision or prime_collision
        self._disallowed_areas = areas

Example 18

Project: meshtool
Source File: load_obj.py
View license
def loadOBJ(data, aux_file_loader=None, validate_output=False):
    """Loads an OBJ file
    
    :param data: A binary data string containing the OBJ file
    :param aux_file_loader: Should be a callable function that takes one parameter.
                            The parameter will be a string containing an auxiliary
                            file that needs to be found, in this case usually a .mtl
                            file or a texture file.
    
    :returns: An instance of :class:`collada.Collada` or None if could not be loaded
    """
    
    mesh = collada.Collada(validate_output=validate_output)
    namer = NameUniqifier()
    material_map = {}
    cimages = []
    materialNamer = NameUniqifier()
    
    vertices = []
    normals = []
    texcoords = []
    
    groups = []
    group = ObjGroup(namer.name("default"))
    geometry_name = namer.name("convertedobjgeometry")
    
    file_like = StringIO(to_unicode(data))
    for line in file_like:
        line = line.strip()
        
        # ignore blank lines and comments
        if len(line) == 0 or line.startswith('#'):
            continue
        
        # split off the first non-whitespace token and ignore the line if there isn't > 1 token
        splitup = line.split(None, 1)
        if len(splitup) != 2:
            continue
        command, line = splitup
        
        if command == 'v':
            line_tokens = line.split()
            vertices.extend(line_tokens[:3])
            
        elif command == 'vn':
            line_tokens = line.split()
            normals.extend(line_tokens[:3])
           
        elif command == 'vt':
            line_tokens = line.split()
            texcoords.extend(line_tokens[:2])
            
        # TODO: other vertex data statements
        # vp
        # cstype
        # deg
        # bmat
        # step
            
        elif command == 'f':
            faces = line.split()
            
            if group.face_mode == FACEMODE.UNKNOWN:
                group.face_mode = detectFaceStyle(faces[0])
                if group.face_mode is None:
                    sys.stderr.write("Error: could not detect face type for line '%s'" % line)
                    return
            
            group.face_lengths.append(len(faces))
            
            # Don't decode the faces here because the / separators have to be parsed out
            # and this is very slow to do one at a time. Instead, just append to a list
            # which is much faster than appending to a string, and it will get joined and
            # parsed later
            group.face_indices.append(line)
        
        elif command == 'l':
            faces = line.split()
            
            if group.face_mode == FACEMODE.UNKNOWN:
                group.face_mode = detectFaceStyle(faces[0])
                if group.face_mode is None:
                    sys.stderr.write("Error: could not detect face type for line '%s'" % line)
                    return
            
            # COLLADA defines lines as a pair of points, so the index values "1 2 3 4" would
            # refer to *two* lines, one between 1 and 2 and one between 3 and 4. OBJ defines
            # lines as continous, so it would be three lines: 1-2, 2-3, 3-4. This duplicates
            # the points to get pairs for COLLADA. This is not very efficient, but not sure
            # of a faster way to do this and I've never seen any files with a huge number of
            # lines in it anyway.
            line = faces[0] + " " + faces[1]
            prev = faces[1]
            for cur in faces[2:]:
                line += " " + prev + " " + cur
                prev = cur
            group.line_indices.append(line)
        
        elif command == 'p':
            faces = line.split()
            
            if group.face_mode == FACEMODE.UNKNOWN:
                group.face_mode = detectFaceStyle(faces[0])
                if group.face_mode is None:
                    sys.stderr.write("Error: could not detect face type for line '%s'" % line)
                    return
                
            # COLLADA does not have points, so this converts a point to a line with two
            # identical endpoints
            line = " ".join(f + " " + f for f in faces)
            group.line_indices.append(line)
        
        # TODO: other elements
        # curv
        # curv2
        # surf
        
        elif command == 'g':
            if group.empty():
                # first group without any previous data, so just set name
                group.name = namer.name(line)
                continue
            
            # end of previous group and start of new group
            groups.append(group)
            group = ObjGroup(namer.name(line))
        
        elif command == 's':
            # there is no way to map shading groups into collada
            continue
        
        elif command == 'o':
            geometry_name = namer.name(line)
        
        # TODO: grouping info
        # mg
        
        # TODO: Free-form curve/surface body statements
        # parm
        # trim
        # hole
        # scrv
        # sp
        # end
        # con
        
        elif command == 'mtllib':
            mtl_file = None
            if aux_file_loader is not None:
                mtl_file = aux_file_loader(line)
            if mtl_file is not None:
                material_data = loadMaterialLib(mtl_file, namer=materialNamer, aux_file_loader=aux_file_loader)
                material_map.update(material_data['material_map'])
                cimages.extend(material_data['images'])
            
        elif command == 'usemtl':
            group.material = slugify(line)
        
        # TODO: display and render attributes
        # bevel
        # c_interp
        # d_interp
        # lod
        # shadow_obj
        # trace_obj
        # ctech
        # stech
        
        else:
            print '  MISSING LINE: %s %s' % (command, line)
    
    # done, append last group
    if not group.empty():
        groups.append(group)
    
    for material in material_map.values():
        mesh.effects.append(material.effect)
        mesh.materials.append(material)
    for cimg in cimages:
        mesh.images.append(cimg)
    
    vertices = numpy.array(vertices, dtype=numpy.float32).reshape(-1, 3)
    normals = numpy.array(normals, dtype=numpy.float32).reshape(-1, 3)
    texcoords = numpy.array(texcoords, dtype=numpy.float32).reshape(-1, 2)
    
    sources = []
    # all modes have vertex source
    sources.append(collada.source.FloatSource("obj-vertex-source", vertices, ('X', 'Y', 'Z')))
    if len(normals) > 0:
        sources.append(collada.source.FloatSource("obj-normal-source", normals, ('X', 'Y', 'Z')))
    if len(texcoords) > 0:
        sources.append(collada.source.FloatSource("obj-uv-source", texcoords, ('S', 'T')))
    
    geom = collada.geometry.Geometry(mesh, geometry_name, geometry_name, sources)
    
    materials_mapped = set()
    for group in groups:
        input_list = collada.source.InputList()
        input_list.addInput(0, 'VERTEX', "#obj-vertex-source")
        if group.face_mode == FACEMODE.VN:
            input_list.addInput(1, 'NORMAL', '#obj-normal-source')
        elif group.face_mode == FACEMODE.VT:
            input_list.addInput(1, 'TEXCOORD', '#obj-uv-source')
        elif group.face_mode == FACEMODE.VTN:
            input_list.addInput(1, 'TEXCOORD', '#obj-uv-source')
            input_list.addInput(2, 'NORMAL', '#obj-normal-source')
        
        if len(group.face_lengths) > 0:
            face_lengths = numpy.array(group.face_lengths, dtype=numpy.int32)
    
            # First, join the individual face lines together, separated by spaces. Then,        
            # just replace 1/2/3 and 1//3 with "1 2 3" and "1  3", as numpy.fromstring can
            # handle any whitespace it's given, similar to python's split(). Concatenating
            # together this way is much faster than parsing the numbers in python - let
            # numpy do it. Note that sep=" " is actually misleading - it handles tabs and
            # other whitespace also
            group.face_indices = (" ".join(group.face_indices)).replace("/", " ")
            face_indices = numpy.fromstring(group.face_indices, dtype=numpy.int32, sep=" ")
            
            # obj indices start at 1, while collada start at 0
            face_indices -= 1
            
            polylist = geom.createPolylist(face_indices, face_lengths, input_list, group.material or namer.name("nullmaterial"))
            geom.primitives.append(polylist)
            
        if len(group.line_indices) > 0:
            group.line_indices = (" ".join(group.line_indices)).replace("/", " ")
            line_indices = numpy.fromstring(group.line_indices, dtype=numpy.int32, sep=" ")
            line_indices -= 1
            lineset = geom.createLineSet(line_indices, input_list, group.material or namer.name("nullmaterial"))
            geom.primitives.append(lineset)
        
        if group.material in material_map:
            materials_mapped.add(group.material)
    
    mesh.geometries.append(geom)
    
    matnodes = []
    for matref in materials_mapped:
        matnode = collada.scene.MaterialNode(matref, material_map[matref], inputs=[('TEX0', 'TEXCOORD', '0')])
        matnodes.append(matnode)
    geomnode = collada.scene.GeometryNode(geom, matnodes)
    node = collada.scene.Node(namer.name("node"), children=[geomnode])
    myscene = collada.scene.Scene(namer.name("scene"), [node])
    mesh.scenes.append(myscene)
    mesh.scene = myscene
    
    return mesh

Example 19

Project: PyParticles
Source File: bubble.py
View license
def bubble():
    """
    Pseudo bubble simulation
    """
    
    ocl_ok = test_pyopencl()
    
    if ocl_ok :
        pcnt = 9000
        r_min=0.5
        occx = occ.OpenCLcontext( pcnt , 3  )
    else :
        pcnt = 700
        r_min = 1.5
    
    steps = 1000000
    dt = 0.01
    
    
    rand_c = rc.RandCluster()
    
    pset = ps.ParticlesSet( pcnt , dtype=np.float32 )
    
    rand_c.insert3( X=pset.X ,
                    M=pset.M ,
                    start_indx=0 ,
                    n=pset.size ,
                    radius=3.0 ,
                    mass_rng=(0.5,0.8) ,
                    r_min=0.0 )
    
    if ocl_ok :
        bubble = pb.PseudoBubbleOCL( pset.size , pset.dim , Consts=(r_min,10) )
        drag = dr.DragOCL( pset.size , pset.dim , Consts=0.01 )
    else :
        bubble = pb.PseudoBubble( pset.size , pset.dim , Consts=(r_min,10) )
        drag = dr.Drag( pset.size , pset.dim , Consts=0.01 )

    constf = cf.ConstForce( pset.size , dim=pset.dim , u_force=[ 0 , 0 , -10.0 ] )
    
    multif = mf.MultipleForce( pset.size , pset.dim )
    multif.append_force( bubble )
    multif.append_force( constf )
    multif.append_force( drag )
    
    multif.set_masses( pset.M )
    
    #solver = els.EulerSolver( multif , pset , dt )
    #solver = lps.LeapfrogSolver( multif , pset , dt )
    solver = svs.StormerVerletSolver( multif , pset , dt )
    #solver = rks.RungeKuttaSolver( lennard_jones , pset , dt )    
    #solver = mds.MidpointSolver( lennard_jones , pset , dt ) 
    
    bound = rb.ReboundBoundary( bound=(-5.0,5.0) )
    
    pset.set_boundary( bound )
    
    a = aogl.AnimatedGl()
    
    a.ode_solver = solver
    a.pset = pset
    a.steps = steps
    
    if ocl_ok :
        a.draw_particles.set_draw_model( a.draw_particles.DRAW_MODEL_VECTOR )
        
    a.init_rotation( -80 , [ 0.7 , 0.05 , 0 ]  )
    
    a.build_animation()
    
    a.start()
    
    return

Example 20

Project: iris
Source File: test_aggregate_by.py
View license
    def setUp(self):
        #
        # common
        #
        cs_latlon = iris.coord_systems.GeogCS(6371229)
        points = np.arange(3, dtype=np.float32) * 3
        coord_lat = iris.coords.DimCoord(points, 'latitude', units='degrees',
                                         coord_system=cs_latlon)
        coord_lon = iris.coords.DimCoord(points, 'longitude', units='degrees',
                                         coord_system=cs_latlon)

        #
        # single coordinate aggregate-by
        #
        a = np.arange(9, dtype=np.int32).reshape(3, 3) + 1
        b = np.arange(36, dtype=np.int32).reshape(36, 1, 1)
        data = b * a

        self.cube_single = iris.cube.Cube(data,
                                          long_name='temperature',
                                          units='kelvin')

        z_points = np.array(
            [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6,
             6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8],
            dtype=np.int32)
        self.coord_z_single = iris.coords.AuxCoord(z_points,
                                                   long_name='height',
                                                   units='m')

        model_level = iris.coords.DimCoord(np.arange(z_points.size),
                                           standard_name='model_level_number')

        self.cube_single.add_aux_coord(self.coord_z_single, 0)
        self.cube_single.add_dim_coord(model_level, 0)
        self.cube_single.add_dim_coord(coord_lon, 1)
        self.cube_single.add_dim_coord(coord_lat, 2)

        #
        # multi coordinate aggregate-by
        #
        a = np.arange(9, dtype=np.int32).reshape(3, 3) + 1
        b = np.arange(20, dtype=np.int32).reshape(20, 1, 1)
        data = b * a

        self.cube_multi = iris.cube.Cube(data, long_name='temperature',
                                         units='kelvin')

        z1_points = np.array(
            [1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 4, 4, 4, 4, 4, 1, 5, 5, 2, 2],
            dtype=np.int32)
        self.coord_z1_multi = iris.coords.AuxCoord(z1_points,
                                                   long_name='height',
                                                   units='m')
        z2_points = np.array(
            [1, 1, 1, 3, 3, 3, 5, 5, 5, 7, 7, 7, 9, 9, 9, 11, 11, 11, 3, 3],
            dtype=np.int32)
        self.coord_z2_multi = iris.coords.AuxCoord(z2_points,
                                                   long_name='level',
                                                   units='1')

        model_level = iris.coords.DimCoord(np.arange(z1_points.size),
                                           standard_name='model_level_number')

        self.cube_multi.add_aux_coord(self.coord_z1_multi, 0)
        self.cube_multi.add_aux_coord(self.coord_z2_multi, 0)
        self.cube_multi.add_dim_coord(model_level, 0)
        self.cube_multi.add_dim_coord(coord_lon.copy(), 1)
        self.cube_multi.add_dim_coord(coord_lat.copy(), 2)

        #
        # expected data results
        #
        self.single_expected = np.array(
            [[[0., 0., 0.],      [0., 0., 0.],        [0., 0., 0.]],
             [[1.5, 3., 4.5],    [6., 7.5, 9.],       [10.5, 12., 13.5]],
             [[4., 8., 12.],     [16., 20., 24.],     [28., 32., 36.]],
             [[7.5, 15., 22.5],  [30., 37.5, 45.],    [52.5, 60., 67.5]],
             [[12., 24., 36.],   [48., 60., 72.],     [84., 96., 108.]],
             [[17.5, 35., 52.5], [70., 87.5, 105.],   [122.5, 140., 157.5]],
             [[24., 48., 72.],   [96., 120., 144.],   [168., 192., 216.]],
             [[31.5, 63., 94.5], [126., 157.5, 189.], [220.5, 252., 283.5]]],
            dtype=np.float64)

        row1 = [[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]
        row2 = [list(np.sqrt([2.5, 10., 22.5])),
                list(np.sqrt([40., 62.5, 90.])),
                list(np.sqrt([122.5, 160., 202.5]))]
        row3 = [list(np.sqrt([16.66666667, 66.66666667, 150.])),
                list(np.sqrt([266.66666667, 416.66666667, 600.])),
                list(np.sqrt([816.66666667, 1066.66666667, 1350.]))]
        row4 = [list(np.sqrt([57.5, 230., 517.5])),
                list(np.sqrt([920., 1437.5, 2070.])),
                list(np.sqrt([2817.5, 3680., 4657.5]))]
        row5 = [list(np.sqrt([146., 584., 1314.])),
                list(np.sqrt([2336., 3650., 5256.])),
                list(np.sqrt([7154., 9344., 11826.]))]
        row6 = [list(np.sqrt([309.16666667, 1236.66666667, 2782.5])),
                list(np.sqrt([4946.66666667, 7729.16666667, 11130.])),
                list(np.sqrt([15149.16666667, 19786.66666667, 25042.5]))]
        row7 = [list(np.sqrt([580., 2320., 5220.])),
                list(np.sqrt([9280., 14500., 20880.])),
                list(np.sqrt([28420., 37120., 46980.]))]
        row8 = [list(np.sqrt([997.5, 3990., 8977.5])),
                list(np.sqrt([15960., 24937.5, 35910.])),
                list(np.sqrt([48877.5, 63840., 80797.5]))]
        self.single_rms_expected = np.array([row1,
                                             row2,
                                             row3,
                                             row4,
                                             row5,
                                             row6,
                                             row7,
                                             row8], dtype=np.float64)

        self.multi_expected = np.array(
            [[[1., 2., 3.],      [4., 5., 6.],     [7., 8., 9.]],
             [[3.5, 7., 10.5],   [14., 17.5, 21.], [24.5, 28., 31.5]],
             [[14., 28., 42.],   [56., 70., 84.],  [98., 112., 126.]],
             [[7., 14., 21.],    [28., 35., 42.],  [49., 56., 63.]],
             [[9., 18., 27.],    [36., 45., 54.],  [63., 72., 81.]],
             [[10.5, 21., 31.5], [42., 52.5, 63.], [73.5, 84., 94.5]],
             [[13., 26., 39.],   [52., 65., 78.],  [91., 104., 117.]],
             [[15., 30., 45.],   [60., 75., 90.],  [105., 120., 135.]],
             [[16.5, 33., 49.5], [66., 82.5, 99.], [115.5, 132., 148.5]]],
            dtype=np.float64)

Example 21

Project: spectral
Source File: spatial.py
View license
    def __call__(self, image, rslice=(None,), cslice=(None,)):
        '''Applies the objects callable function to the image data.

        Arguments:

            `image` (numpy.ndarray):

                An image with shape (R, C, B).

            `rslice` (tuple):

                Tuple of `slice` parameters specifying at which rows the function
                should be applied. If not provided, `func` is applied to all rows.

            `cslice` (tuple):

                Tuple of `slice` parameters specifying at which columns the
                function should be applied. If not provided, `func` is applied to
                all columns.

        Returns numpy.ndarray:

            An array whose elements correspond to the outputs from the
            object's callable function.
        '''
        import spectral
        from spectral.algorithms.algorithms import GaussianStats
        (R, C, B) = image.shape
        (row_border, col_border) = [x // 2 for x in self.outer]

        if self.dim_out is not None:
            dim_out = self.dim_out
        elif hasattr(self.callable, 'dim_out') and \
          self.callable.dim_out is not None:
            dim_out = self.callable.dim_out
        else:
            dim_out = 1

        # Row/Col indices at which to apply the windowed function
        rvals = list(range(*slice(*rslice).indices(R)))
        cvals = list(range(*slice(*cslice).indices(C)))

        nrows_out = len(rvals)
        ncols_out = len(cvals)

        if dim_out > 1:
            x = np.ones((nrows_out, ncols_out, dim_out),
                        dtype=np.float32) * -1.0
        else:
            x = np.ones((nrows_out, ncols_out), dtype=self.dtype) * -1.0

        npixels = self.outer[0] * self.outer[1] - self.inner[0] * self.inner[1]
        if self.cov is None and npixels < B:
            raise ValueError('Window size provides too few samples for ' \
                             'image data dimensionality.')

        if self.create_mask is not None:
            create_mask = self.create_mask
        else:
            create_mask = inner_outer_window_mask_creator(image.shape,
                                                          self.inner,
                                                          self.outer)

        interior_mask = create_mask(R // 2, C // 2, True)[2].ravel()
        interior_indices = np.argwhere(interior_mask == 0).squeeze()

        (i_interior_start, i_interior_stop) = (row_border, R - row_border)
        (j_interior_start, j_interior_stop) = (col_border, C - col_border)

        status = spectral._status
        status.display_percentage('Processing image: ')
        if self.cov is not None:
            # Since we already have the covariance, just use np.mean to get
            # means of the inner window and outer (including the inner), then
            # use those to calculate the mean of the outer window alone.
            background = GaussianStats(cov=self.cov)
            for i in range(nrows_out):
                for j in range(ncols_out):
                    (inner, outer) = create_mask(rvals[i], cvals[j], False)
                    N_in = (inner[1] - inner[0]) * (inner[3] - inner[2])
                    N_tot = (outer[1] - outer[0]) * (outer[3] - outer[2])
                    mean_out = np.mean(image[outer[0]: outer[1],
                                             outer[2]: outer[3]].reshape(-1, B),
                                             axis=0)
                    mean_in = np.mean(image[outer[0]: outer[1],
                                            outer[2]: outer[3]].reshape(-1, B),
                                            axis=0)
                    mean = mean_out * (float(N_tot) / (N_tot - N_in)) - \
                           mean_in * (float(N_in) / (N_tot - N_in))
                    background.mean = mean
                    x[i, j] = self.callable(background,
                                            image[rvals[i], cvals[j]])
                if i % (nrows_out // 10) == 0:
                    status.update_percentage(100. * i // nrows_out)
        else:
            # Need to calculate both the mean and covariance for the outer
            # window (without the inner).
            (h, w) = self.outer[:]
            for i in range(nrows_out):
                ii = rvals[i] - h // 2
                for j in range(ncols_out):
                    jj = cvals[j] - w // 2
                    if i_interior_start <= rvals[i] < i_interior_stop and \
                        j_interior_start <= cvals[j] < j_interior_stop:
                        X = image[ii : ii + h, jj : jj + w, :]
                        indices = interior_indices
                    else:
                        (inner, (i0, i1, j0, j1), mask) = \
                          create_mask(rvals[i], cvals[j], True)
                        indices = np.argwhere(mask.ravel() == 0).squeeze()
                        X = image[i0 : i1, j0 : j1, :]
                    X = np.take(X.reshape((-1, B)), indices, axis=0)
                    mean = np.mean(X, axis=0)
                    cov = np.cov(X, rowvar=False)
                    background = GaussianStats(mean, cov)
                    x[i, j] = self.callable(background,
                                            image[rvals[i], cvals[j]])
                if i % (nrows_out // 10) == 0:
                    status.update_percentage(100. * i / nrows_out)

        status.end_percentage()
        return x

Example 22

Project: blog
Source File: train_hf.py
View license
def test_plant():
    """Example of a network using a dynamic plant as the output layer."""

    eps = 1e-6  # value to use for finite differences computations
    dt = 1e-2  # size of time step
    sig_len = 40  # how many time steps to train over
    batch_size = 32  # how many updates to perform with static input
    num_batches = 20000  # how many batches to run total

    import sys
    # NOTE: Change to wherever you keep your arm models
    sys.path.append("../../../studywolf_control/studywolf_control/")
    from arms.two_link.arm_python import Arm as Arm
    print('Plant is: %s' % str(Arm))
    arm = Arm(dt=dt)

    num_states = arm.DOF * 2  # are states are [positions, velocities]
    targets = gen_targets(arm=arm, sig_len=sig_len)  # target joint angles
    init_state = np.zeros((len(targets), num_states), dtype=np.float32)
    init_state[:, :arm.DOF] = arm.init_q  # set up the initial joint angles
    plant = PlantArm(arm=arm, targets=targets,
                     init_state=init_state, eps=eps)

    # open up weights folder and checked for saved weights
    import glob
    folder = 'weights'
    files = sorted(glob.glob('%s/rnn*' % folder))
    if len(files) > 0:
        # if weights found, load them up and keep going from last trial
        W = np.load(files[-1])['arr_0']
        print('loading from %s' % files[-1])
        last_trial = int(files[-1].split('%s/rnn_weights-trial' %
                                         folder)[1].split('-err')[0])
        print('last_trial: %i' % last_trial)
    else:
        # if no weights found, start fresh with new random seed
        W = None
        last_trial = -1
        seed = np.random.randint(100000000)
        print('seed : %i' % seed)
        np.random.seed(seed)

    # specify the network structure and loss functions
    from hessianfree.loss_funcs import SquaredError, SparseL2
    net_size = 32
    rnn = RNNet(
        # specify the number of nodes in each layer
        shape=[num_states * 2,
               net_size,
               net_size,
               num_states,
               num_states],
        # specify the function of the nodes in each layer
        layers=[Linear(), Tanh(), Tanh(), Linear(), plant],
        # specify the layers that have recurrent connections
        rec_layers=[1, 2],
        # specify the connections between layers
        conns={0: [1, 2], 1: [2], 2: [3], 3: [4]},
        # specify the loss function
        loss_type=[
            # squared error between plant output and targets
            SquaredError()],
        load_weights=W,
        use_GPU=False)

    # set up masking so that weights between network output
    # and the plant aren't modified in learning, always = 1
    offset, W_end, b_end = rnn.offsets[(3, 4)]
    rnn.mask = np.zeros(rnn.W.shape, dtype=bool)
    rnn.mask[offset:b_end] = True
    rnn.W[offset:W_end] = np.eye(num_states, dtype=np.float32).flatten()

    for ii in range(last_trial+1, num_batches):
        print('=============================================')
        print('training batch %i' % ii)
        err = rnn.run_epochs(plant, None, max_epochs=batch_size,
                             optimizer=HessianFree(CG_iter=96,
                                                   init_damping=100))
        # save the weights to file, track trial and error
        err = rnn.best_error
        name = '%s/rnn_weights-trial%04i-err%.5f' % (folder, ii, err)
        np.savez_compressed(name, rnn.W)
        print('=============================================')
        print('network: %s' % name)
        print('final error: %f' % err)
        print('=============================================')

    return rnn.best_error

Example 23

Project: galry
Source File: plot_visual.py
View license
    def initialize(self, x=None, y=None, color=None, point_size=1.0,
            position=None, nprimitives=None, index=None,
            color_array_index=None, thickness=None,
            options=None, autocolor=None, autonormalizable=True):
            
        # if position is specified, it contains x and y as column vectors
        if position is not None:
            position = np.array(position, dtype=np.float32)
            if thickness:
                shape = (2 * position.shape[0], 1)
            else:
                shape = (1, position.shape[0])
        else:
            position, shape = process_coordinates(x=x, y=y)
            if thickness:
                shape = (shape[0], 2 * shape[1])
        
        
        # register the size of the data
        self.size = np.prod(shape)
        
        # there is one plot per row
        if not nprimitives:
            nprimitives = shape[0]
            nsamples = shape[1]
        else:
            nsamples = self.size // nprimitives
        
        
        # handle thickness
        if thickness and position.shape[0] >= 2:
            w = thickness
            n = self.size
            X = position
            Y = np.zeros((n, 2))
            u = np.zeros((n/2, 2))
            X2 = np.vstack((X, 2*X[-1,:]-X[-2,:]))
            u[:,0] = -np.diff(X2[:,1])
            u[:,1] = np.diff(X2[:,0])
            r = (u[:,0] ** 2 + u[:,1] ** 2) ** .5
            rm = r.mean()
            r[r == 0.] = rm
            # print u
            # print r
            # ind = np.nonzero(r == 0.)[0]
            # print ind, ind-1
            # r[ind] = r[ind - 1]
            u[:,0] /= r
            u[:,1] /= r
            Y[::2,:] = X - w * u
            Y[1::2,:] = X + w * u
            position = Y
            x = Y[:,0]
            y = Y[:,1]
            # print x
            # print y
            self.primitive_type = 'TRIANGLE_STRIP'
            
            
        # register the bounds
        if nsamples <= 1:
            self.bounds = [0, self.size]
        else:
            self.bounds = np.arange(0, self.size + 1, nsamples)
        
        # normalize position
        # if viewbox:
            # self.add_normalizer('position', viewbox)
        
        # by default, use the default color
        if color is None:
            if nprimitives <= 1:
                color = self.default_color
        
        # automatic color with color map
        if autocolor is not None:
            if nprimitives <= 1:
                color = get_next_color(autocolor)
            else:
                color = [get_next_color(i + autocolor) for i in xrange(nprimitives)]
            
            
        # # handle the case where the color is a string where each character
        # # is a color (eg. 'ry')
        # if isinstance(color, basestring):
            # color = list(color)
        color = get_color(color)
        # handle the case where there is a single color given as a list of
        # RGB components instead of a tuple
        if type(color) is list:
            if color and (type(color[0]) != tuple) and (3 <= len(color) <= 4):
                color = tuple(color)
            else:
                color = np.array(color)
        # first, initialize use_color_array to False except if
        # color_array_index is not None
        use_color_array = color_array_index is not None
        if isinstance(color, np.ndarray):
            colors_ndim = color.shape[1]
            # first case: one color per point
            if color.shape[0] == self.size:
                single_color = False
            # second case: use a color array so that each plot has a single
            # color, this saves memory since there is a single color in
            # memory for any plot
            else:
                use_color_array = True
                single_color = False
        elif type(color) is tuple:
            single_color = True
            colors_ndim = len(color)
        
        # set position attribute
        self.add_attribute("position", ndim=2, data=position, 
            autonormalizable=autonormalizable)
        
        if index is not None:
            index = np.array(index)
            # self.size = len(index)
            self.add_index("index", data=index)
        
        # single color case: no need for a color buffer, just use default color
        if single_color and not use_color_array:
            self.add_uniform("color", ndim=colors_ndim, data=color)
            if colors_ndim == 3:
                self.add_fragment_main("""
            out_color = vec4(color, 1.0);
                """)
            elif colors_ndim == 4:
                self.add_fragment_main("""
            out_color = color;
                """)
        
        # multiple colors case: color attribute
        elif not use_color_array:
            self.add_attribute("color", ndim=colors_ndim, data=color)
            self.add_varying("varying_color", vartype="float", ndim=colors_ndim)
            
            self.add_vertex_main("""
            varying_color = color;
            """)
            
            if colors_ndim == 3:
                self.add_fragment_main("""
            out_color = vec4(varying_color, 1.0);
                """)
            elif colors_ndim == 4:
                self.add_fragment_main("""
            out_color = varying_color;
                """)
        
        # multiple colors, but with a color array to save memory
        elif use_color_array:
            if color_array_index is None:
                color_array_index = np.repeat(np.arange(nprimitives), nsamples)
            color_array_index = np.array(color_array_index)
                
            ncolors = color.shape[0]
            ncomponents = color.shape[1]
            color = color.reshape((1, ncolors, ncomponents))
            
            dx = 1. / ncolors
            offset = dx / 2.
            
            self.add_texture('colormap', ncomponents=ncomponents, ndim=1, data=color)
            self.add_attribute('index', ndim=1, vartype='int', data=color_array_index)
            self.add_varying('vindex', vartype='int', ndim=1)
            
            self.add_vertex_main("""
            vindex = index;
            """)
            
            self.add_fragment_main("""
            float coord = %.5f + vindex * %.5f;
            vec4 color = texture1D(colormap, coord);
            out_color = color;
            """ % (offset, dx))

        # add point size uniform (when it's not specified, there might be some
        # bugs where its value is obtained from other datasets...)
        self.add_uniform("point_size", data=point_size)
        self.add_vertex_main("""gl_PointSize = point_size;""")

Example 24

Project: intrinsic
Source File: energy.py
View license
    def compute_unary_costs(self, decomposition, prev_decomposition):
        """ Returns unary costs: nnz x nlabels matrix """

        if self.params.logging:
            t0 = timeit.default_timer()
            print("compute_unary_costs...")

        intensities = decomposition.intensities
        chromaticities = decomposition.chromaticities
        nlabels = intensities.shape[0]
        unary_costs = np.zeros(
            (self.input.mask_nnz, nlabels),
            dtype=np.float32)

        sigma_spatial = (
            self.params.shading_blur_sigma *
            self.input.diag / (
                1.0 + decomposition.iter_num **
                self.params.shading_blur_iteration_pow
            )
        )
        if self.params.logging:
            print('blur sigma: %s pixels (image diagonal: %s pixels)' %
                  (sigma_spatial, self.input.diag))

        # obtain previous shading layer, or use a method to create a proxy
        if prev_decomposition:
            prev_r_nz, prev_s_nz = prev_decomposition.get_r_s_nz()
        elif self.params.shading_blur_init_method == "constant":
            prev_s_nz = 0.5 * np.ones_like(self.input.image_gray_nz)
        elif self.params.shading_blur_init_method == "image":
            prev_s_nz = self.input.image_gray_nz
        elif self.params.shading_blur_init_method == "none":
            prev_s_nz = None
        else:
            raise ValueError("Unknown shading_blur_init_method: %s" %
                             self.params.shading_blur_init_method)

        if prev_s_nz is not None:
            if self.params.shading_blur_log:
                # blur in log space
                blur_input = np.log(prev_s_nz)
            else:
                # blur in linear space, then convert to log
                blur_input = prev_s_nz

            blur_output = gaussian_blur_gray_image_nz(
                image_nz=blur_input,
                image_shape=self.input.shape,
                mask_nz=self.input.mask_nz,
                sigma=sigma_spatial,
            )

            if self.params.shading_blur_log:
                log_s_target_nz = blur_output
            else:
                log_s_target_nz = np.log(blur_output)
        else:
            log_s_target_nz = None

        # (used below)
        if self.params.shading_target_chromaticity:
            labels_rgb = np.clip(
                decomposition.get_reflectances_rgb(), 1e-5, np.inf)

        # shading and chromaticity terms
        for i in xrange(nlabels):
            s_nz = self.input.image_gray_nz / intensities[i]
            r_nz = (self.input.image_rgb_nz /
                    np.clip(s_nz, 1e-4, 1e5)[:, np.newaxis])

            # absolute reflectance and shading
            unary_costs[:, i] += (
                self.prob_abs_s.cost(s_nz) +
                self.prob_abs_r.cost(r_nz)
            )

            # chromaticity: encourage reflectance intensities to be assigned to
            # pixels that share the same chromaticity as the original kmeans
            # cluster from which the reflectance intensity was obtained.
            if self.params.chromaticity_weight:
                if self.params.chromaticity_norm == "L1":
                    f = np.abs
                elif self.params.chromaticity_norm == "L2":
                    f = np.square
                else:
                    raise ValueError(
                        "Invalid value of chromaticity_norm: %s" %
                        self.params.chromaticity_norm)

                unary_costs[:, i] += self.params.chromaticity_weight * (
                    np.sum(
                        f(self.input.image_irg_nz[:, 1:3] -
                          chromaticities[i, :]),
                        axis=1
                    )
                )

            # shading smoothness: discourage shading discontinuities
            if self.params.shading_target_weight and log_s_target_nz is not None:
                if self.params.shading_target_norm == "L2":
                    f = np.square
                elif self.params.shading_target_norm == "L1":
                    f = np.abs
                else:
                    raise ValueError("Invalid value of shading_target_norm: %s" %
                                     self.params.shading_target_norm)

                if self.params.shading_target_chromaticity:
                    # interpret labels as RGB (intensity with chromaticity),
                    # thereby penalizing deviations from grayscale in the
                    # shading channel (though the final answer is always
                    # grayscale anyway)
                    label_rgb = labels_rgb[i, :]
                    s_rgb_nz = self.input.image_rgb_nz / label_rgb[np.newaxis, :]
                    log_s_rgb_nz = np.log(np.clip(s_rgb_nz, 1e-5, np.inf))
                    unary_costs[:, i] += (
                        self.params.shading_target_weight *
                        np.sum(f(log_s_rgb_nz - log_s_target_nz[:, np.newaxis]), axis=-1)
                    )
                else:
                    # interpret labels as intensities
                    log_s_nz = np.log(s_nz)
                    unary_costs[:, i] += (
                        self.params.shading_target_weight *
                        f(log_s_nz - log_s_target_nz)
                    )

        if self.params.logging:
            t1 = timeit.default_timer()
            print("compute_unary_costs: done (%s s)" % (t1 - t0))

        return unary_costs

Example 25

Project: rcnn
Source File: logistic_regression.py
View license
    def train(self):
        args = self.args
        train_x, train_y = self.train_set
        dev_x, dev_y = self.dev_set
        test_x, test_y = self.test_set

        updates, lr, gnorm = create_optimization_updates(
                cost = self.cost,
                params = self.params,
                lr = args.learning_rate,
                rho = args.rho,
                beta1 = args.beta1,
                beta2 = args.beta2,
                momentum = args.momentum,
                gamma = args.gamma,
                method = args.learning
            )[:3]

        batch = args.batch
        index = self.index
        x = self.x
        y = self.y

        train_func = theano.function(
                inputs = [ index ],
                outputs = [ self.cost, gnorm ],
                givens = {
                    x: train_x[index*batch:(index+1)*batch],
                    y: train_y[index*batch:(index+1)*batch]
                },
                updates = updates
            )

        dev_func = theano.function(
                inputs = [ index ],
                outputs = [ self.err, self.loss ],
                givens = {
                    x: dev_x[index*batch:(index+1)*batch],
                    y: dev_y[index*batch:(index+1)*batch]
                }
            )

        test_func = theano.function(
                inputs = [ index ],
                outputs = [ self.err, self.loss ],
                givens = {
                    x: test_x[index*batch:(index+1)*batch],
                    y: test_y[index*batch:(index+1)*batch]
                }
            )

        decay_lr = args.decay_lr and args.learning.lower() != "adadelta" and \
                        args.learning.lower() != "adagrad"
        lr_0 = args.learning_rate
        iter_cnt = 0

        N = train_x.get_value(borrow=True).shape[0]
        num_batches = (N-1)/batch + 1
        processed = 0
        period = args.eval_period

        best_dev_err = 1.0

        max_epochs = args.max_epochs
        for epoch in xrange(max_epochs):
            start_time = time.time()
            tot_cost = 0
            for i in xrange(num_batches):
                iter_cnt += 1
                if decay_lr:
                    lr.set_value(np.float32(lr_0/iter_cnt**0.5))
                cost, grad_norm = train_func(i)
                tot_cost += cost

                if math.isnan(cost):
                    say("NaN !!\n")
                    return

                ed = min(N, (i+1)*batch)
                prev = processed/period
                processed += ed-i*batch

                if (i == num_batches-1) or (processed/period > prev):
                    say("Epoch={:.1f} Sample={} cost={:.4f} |g|={:.2f}\t[{:.1f}m]\n".format(
                            epoch + (i+1.0)/num_batches,
                            processed,
                            tot_cost/(i+1),
                            float(grad_norm),
                            (time.time()-start_time)/60.0
                        ))
                    dev_err, dev_loss = self.evaluate(dev_func, dev_x)
                    best_dev_err = min(best_dev_err, dev_err)
                    say("\tdev_err={:.4f} dev_loss={:.4f} best_dev={:.4f}\n".format(
                            dev_err, dev_loss, best_dev_err))
                    if dev_err == best_dev_err:
                        test_err, test_loss = self.evaluate(test_func, test_x)
                        say("\ttest_err={:.4f} test_loss={:.4f}\n".format(
                                test_err, test_loss))
                    say("\n")

Example 26

Project: models
Source File: deep_cnn.py
View license
def train(images, labels, ckpt_path, dropout=False):
  """
  This function contains the loop that actually trains the model.
  :param images: a numpy array with the input data
  :param labels: a numpy array with the output labels
  :param ckpt_path: a path (including name) where model checkpoints are saved
  :param dropout: Boolean, whether to use dropout or not
  :return: True if everything went well
  """

  # Check training data
  assert len(images) == len(labels)
  assert images.dtype == np.float32
  assert labels.dtype == np.int32

  # Set default TF graph
  with tf.Graph().as_default():
    global_step = tf.Variable(0, trainable=False)

    # Declare data placeholder
    train_data_node = _input_placeholder()

    # Create a placeholder to hold labels
    train_labels_shape = (FLAGS.batch_size,)
    train_labels_node = tf.placeholder(tf.int32, shape=train_labels_shape)

    print("Done Initializing Training Placeholders")

    # Build a Graph that computes the logits predictions from the placeholder
    if FLAGS.deeper:
      logits = inference_deeper(train_data_node, dropout=dropout)
    else:
      logits = inference(train_data_node, dropout=dropout)

    # Calculate loss
    loss = loss_fun(logits, train_labels_node)

    # Build a Graph that trains the model with one batch of examples and
    # updates the model parameters.
    train_op = train_op_fun(loss, global_step)

    # Create a saver.
    saver = tf.train.Saver(tf.all_variables())

    print("Graph constructed and saver created")

    # Build an initialization operation to run below.
    init = tf.initialize_all_variables()

    # Create and init sessions
    sess = tf.Session(config=tf.ConfigProto(log_device_placement=FLAGS.log_device_placement)) #NOLINT(long-line)
    sess.run(init)

    print("Session ready, beginning training loop")

    # Initialize the number of batches
    data_length = len(images)
    nb_batches = math.ceil(data_length / FLAGS.batch_size)

    for step in xrange(FLAGS.max_steps):
      # for debug, save start time
      start_time = time.time()

      # Current batch number
      batch_nb = step % nb_batches

      # Current batch start and end indices
      start, end = utils.batch_indices(batch_nb, data_length, FLAGS.batch_size)

      # Prepare dictionnary to feed the session with
      feed_dict = {train_data_node: images[start:end],
                   train_labels_node: labels[start:end]}

      # Run training step
      _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)

      # Compute duration of training step
      duration = time.time() - start_time

      # Sanity check
      assert not np.isnan(loss_value), 'Model diverged with loss = NaN'

      # Echo loss once in a while
      if step % 100 == 0:
        num_examples_per_step = FLAGS.batch_size
        examples_per_sec = num_examples_per_step / duration
        sec_per_batch = float(duration)

        format_str = ('%s: step %d, loss = %.2f (%.1f examples/sec; %.3f '
                      'sec/batch)')
        print (format_str % (datetime.now(), step, loss_value,
                             examples_per_sec, sec_per_batch))

      # Save the model checkpoint periodically.
      if step % 1000 == 0 or (step + 1) == FLAGS.max_steps:
        saver.save(sess, ckpt_path, global_step=step)

  return True

Example 27

Project: kaggle-Rain
Source File: NNregression_v2.py
View license
def do_regression(num_epochs=60, # No. of epochs to train
                  init_file=None,  # Saved parameters to initialise training
                  epoch_size=680780,  # Whole dataset size
                  valid_size=34848, # Size of validation holdout set
                  train_batch_multiple=10637,  # No. of minibatches per batch
                  valid_batch_multiple=1089,  # No. of minibatches per batch
                  train_minibatch_size=64,
                  valid_minibatch_size=32,
                  eval_multiple=50,  # No. of minibatches to ave. in report
                  save_model=True,
                  input_width=19,
                  rng_seed=100005,
                  cross_val=0,  # Cross-validation subset label
                  dataver=2,  # Label for different runs/architectures/etc
                  rate_init=1.0,
                  rate_decay=0.999983):

    ###################################################
    ################# 0. User inputs ##################
    ###################################################
    for i in range(1,len(sys.argv)):
        if sys.argv[i].startswith('-'):
            option = sys.argv[i][1:]
            if option == 'i': init_file = sys.argv[i+1]
            elif option[0:2] == 'v=' : dataver = int(option[2:])
            elif option[0:3] == 'cv=' : cross_val = int(option[3:])
            elif option[0:3] == 'rs=' : rng_seed = int(option[3:])
            elif option[0:3] == 'ri=' : rate_init = np.float32(option[3:])
            elif option[0:3] == 'rd=' : rate_decay = np.float32(option[3:])
                                
    print("Running with dataver %s" % (dataver))
    print("Running with cross_val %s" % (cross_val))
    
    
    ###################################################
    ############# 1. Housekeeping values ##############
    ###################################################
    # Batch size is possibly not equal to epoch size due to memory limits
    train_batch_size = train_batch_multiple*train_minibatch_size 
    assert epoch_size >= train_batch_size
    
    # Number of times we expect the training/validation generator to be called
    max_train_gen_calls = (num_epochs*epoch_size)//train_batch_size 

    # Number of evaluations (total minibatches / eval_multiple)
    num_eval = max_train_gen_calls*train_batch_multiple / eval_multiple
    
    
    ###################################################
    ###### 2. Define model and theano variables #######
    ###################################################
    if rng_seed is not None:
        print("Setting RandomState with seed=%i" % (rng_seed))
        rng = np.random.RandomState(rng_seed)
        set_rng(rng)
    
    print("Defining variables...")
    index = T.lscalar() # Minibatch index
    x = T.tensor3('x') # Inputs 
    y = T.fvector('y') # Target
    
    print("Defining model...")
    network_0 = build_1Dregression_v2(
                        input_var=x,
                        input_width=input_width,
                        h_num_units=[120,120,120],
                        h_grad_clip=1.0,
                        output_width=1
                        )
                        
    if init_file is not None:
        print("Loading initial model parametrs...")
        init_model = np.load(init_file)
        init_params = init_model[init_model.files[0]]           
        LL.set_all_param_values([network_0], init_params)
        
    
    ###################################################                                
    ################ 3. Import data ###################
    ###################################################
    # Loading data generation model parameters
    print("Defining shared variables...")
    train_set_y = theano.shared(np.zeros(1, dtype=theano.config.floatX),
                                borrow=True) 
    train_set_x = theano.shared(np.zeros((1,1,1), dtype=theano.config.floatX),
                                borrow=True)
    
    valid_set_y = theano.shared(np.zeros(1, dtype=theano.config.floatX),
                                borrow=True)
    valid_set_x = theano.shared(np.zeros((1,1,1), dtype=theano.config.floatX),
                                borrow=True)
    
    # Validation data (pick a single augmented instance, rand0 here)
    print("Creating validation data...")    
    chunk_valid_data = np.load(
        "./valid/data_valid_augmented_cv%s_t%s_rand0.npy" 
        % (cross_val, input_width)
        ).astype(theano.config.floatX)
    chunk_valid_answers = np.load(
        "./valid/data_valid_expected_cv%s.npy" 
        % (cross_val)
        ).astype(theano.config.floatX)     
    
    print "chunk_valid_answers.shape", chunk_valid_answers.shape
    print("Assigning validation data...")
    valid_set_y.set_value(chunk_valid_answers[:])
    valid_set_x.set_value(chunk_valid_data.transpose(0,2,1))
    
    # Create output directory
    if not os.path.exists("output_cv%s_v%s" % (cross_val, dataver)):
        os.makedirs("output_cv%s_v%s" % (cross_val, dataver))
    
    
    ###################################################                                
    ########### 4. Create Loss expressions ############
    ###################################################
    print("Defining loss expressions...")
    prediction_0 = LL.get_output(network_0) 
    train_loss = aggregate(T.abs_(prediction_0 - y.dimshuffle(0,'x')))
    
    valid_prediction_0 = LL.get_output(network_0, deterministic=True)
    valid_loss = aggregate(T.abs_(valid_prediction_0 - y.dimshuffle(0,'x')))
    
    
    ###################################################                                
    ############ 5. Define update method  #############
    ###################################################
    print("Defining update choices...")
    params = LL.get_all_params(network_0, trainable=True)
    learn_rate = T.scalar('learn_rate', dtype=theano.config.floatX)
    
    updates = lasagne.updates.adadelta(train_loss, params,
                                       learning_rate=learn_rate)
    
    
    ###################################################                                
    ######### 6. Define train/valid functions #########
    ###################################################    
    print("Defining theano functions...")
    train_model = theano.function(
        [index, learn_rate],
        train_loss,
        updates=updates,
        givens={
            x: train_set_x[(index*train_minibatch_size):
                            ((index+1)*train_minibatch_size)],
            y: train_set_y[(index*train_minibatch_size):
                            ((index+1)*train_minibatch_size)]  
        }
    )
    
    validate_model = theano.function(
        [index],
        valid_loss,
        givens={
            x: valid_set_x[index*valid_minibatch_size:
                            (index+1)*valid_minibatch_size],
            y: valid_set_y[index*valid_minibatch_size:
                            (index+1)*valid_minibatch_size]
        }
    )
    
    
    ###################################################                                
    ################ 7. Begin training ################
    ###################################################  
    print("Begin training...")
    sys.stdout.flush()
    
    cum_iterations = 0
    this_train_loss = 0.0
    this_valid_loss = 0.0
    best_valid_loss = np.inf
    best_iter = 0
    
    train_eval_scores = np.empty(num_eval)
    valid_eval_scores = np.empty(num_eval)
    eval_index = 0
    aug_index = 0
    
    for batch in xrange(max_train_gen_calls):
        start_time = time.time()        
        chunk_train_data = np.load(
            "./train/data_train_augmented_cv%s_t%s_rand%s.npy" %
            (cross_val, input_width, aug_index)
            ).astype(theano.config.floatX)
        chunk_train_answers = np.load(
            "./train/data_train_expected_cv%s.npy" % 
            (cross_val)
            ).astype(theano.config.floatX)     
            
        train_set_y.set_value(chunk_train_answers[:])
        train_set_x.set_value(chunk_train_data.transpose(0, 2, 1))
        
        # Iterate over minibatches in each batch
        for mini_index in xrange(train_batch_multiple):
            this_rate = np.float32(rate_init*(rate_decay**cum_iterations))
            this_train_loss += train_model(mini_index, this_rate)
            cum_iterations += 1
            
            # Report loss 
            if (cum_iterations % eval_multiple == 0):
                this_train_loss = this_train_loss / eval_multiple
                this_valid_loss = np.mean([validate_model(i) for
                                    i in xrange(valid_batch_multiple)])
                train_eval_scores[eval_index] = this_train_loss
                valid_eval_scores[eval_index] = this_valid_loss
                
                # Save report every five evaluations
                if ((eval_index+1) % 5 == 0):
                    np.savetxt(
                        "output_cv%s_v%s/training_scores.txt" %
                        (cross_val, dataver),
                         train_eval_scores, fmt="%.5f"
                         )
                    np.savetxt(
                        "output_cv%s_v%s/validation_scores.txt" %
                        (cross_val, dataver),
                         valid_eval_scores, fmt="%.5f"
                         )
                    np.savetxt(
                        "output_cv%s_v%s/last_learn_rate.txt" %
                        (cross_val, dataver),
                        [np.array(this_rate)], fmt="%.5f"
                        )
                
                # Save model if best validation score
                if (this_valid_loss < best_valid_loss):  
                    best_valid_loss = this_valid_loss
                    best_iter = cum_iterations-1
                    
                    if save_model:
                        np.savez("output_cv%s_v%s/model.npz" % 
                                 (cross_val, dataver),
                                 LL.get_all_param_values(network_0))
                    
                # Reset evaluation reports
                eval_index += 1
                this_train_loss = 0.0
                this_valid_loss = 0.0
                
        aug_index += 1
            
        end_time = time.time()
        print("Computing time for batch %d: %f" % (batch, end_time-start_time))
        
    print("Best validation loss %f after %d epochs" %
          (best_valid_loss, (best_iter*train_minibatch_size//epoch_size)))
    
    del train_set_x, train_set_y, valid_set_x, valid_set_y
    gc.collect()
    
    return None

Example 28

Project: scipy
Source File: pilutil.py
View license
def toimage(arr, high=255, low=0, cmin=None, cmax=None, pal=None,
            mode=None, channel_axis=None):
    """Takes a numpy array and returns a PIL image.

    The mode of the PIL image depends on the array shape and the `pal` and
    `mode` keywords.

    For 2-D arrays, if `pal` is a valid (N,3) byte-array giving the RGB values
    (from 0 to 255) then ``mode='P'``, otherwise ``mode='L'``, unless mode
    is given as 'F' or 'I' in which case a float and/or integer array is made.

    Notes
    -----
    For 3-D arrays, the `channel_axis` argument tells which dimension of the
    array holds the channel data.

    For 3-D arrays if one of the dimensions is 3, the mode is 'RGB'
    by default or 'YCbCr' if selected.

    The numpy array must be either 2 dimensional or 3 dimensional.

    """
    data = asarray(arr)
    if iscomplexobj(data):
        raise ValueError("Cannot convert a complex-valued array.")
    shape = list(data.shape)
    valid = len(shape) == 2 or ((len(shape) == 3) and
                                ((3 in shape) or (4 in shape)))
    if not valid:
        raise ValueError("'arr' does not have a suitable array shape for "
                         "any mode.")
    if len(shape) == 2:
        shape = (shape[1], shape[0])  # columns show up first
        if mode == 'F':
            data32 = data.astype(numpy.float32)
            image = Image.frombytes(mode, shape, data32.tostring())
            return image
        if mode in [None, 'L', 'P']:
            bytedata = bytescale(data, high=high, low=low,
                                 cmin=cmin, cmax=cmax)
            image = Image.frombytes('L', shape, bytedata.tostring())
            if pal is not None:
                image.putpalette(asarray(pal, dtype=uint8).tostring())
                # Becomes a mode='P' automagically.
            elif mode == 'P':  # default gray-scale
                pal = (arange(0, 256, 1, dtype=uint8)[:, newaxis] *
                       ones((3,), dtype=uint8)[newaxis, :])
                image.putpalette(asarray(pal, dtype=uint8).tostring())
            return image
        if mode == '1':  # high input gives threshold for 1
            bytedata = (data > high)
            image = Image.frombytes('1', shape, bytedata.tostring())
            return image
        if cmin is None:
            cmin = amin(ravel(data))
        if cmax is None:
            cmax = amax(ravel(data))
        data = (data*1.0 - cmin)*(high - low)/(cmax - cmin) + low
        if mode == 'I':
            data32 = data.astype(numpy.uint32)
            image = Image.frombytes(mode, shape, data32.tostring())
        else:
            raise ValueError(_errstr)
        return image

    # if here then 3-d array with a 3 or a 4 in the shape length.
    # Check for 3 in datacube shape --- 'RGB' or 'YCbCr'
    if channel_axis is None:
        if (3 in shape):
            ca = numpy.flatnonzero(asarray(shape) == 3)[0]
        else:
            ca = numpy.flatnonzero(asarray(shape) == 4)
            if len(ca):
                ca = ca[0]
            else:
                raise ValueError("Could not find channel dimension.")
    else:
        ca = channel_axis

    numch = shape[ca]
    if numch not in [3, 4]:
        raise ValueError("Channel axis dimension is not valid.")

    bytedata = bytescale(data, high=high, low=low, cmin=cmin, cmax=cmax)
    if ca == 2:
        strdata = bytedata.tostring()
        shape = (shape[1], shape[0])
    elif ca == 1:
        strdata = transpose(bytedata, (0, 2, 1)).tostring()
        shape = (shape[2], shape[0])
    elif ca == 0:
        strdata = transpose(bytedata, (1, 2, 0)).tostring()
        shape = (shape[2], shape[1])
    if mode is None:
        if numch == 3:
            mode = 'RGB'
        else:
            mode = 'RGBA'

    if mode not in ['RGB', 'RGBA', 'YCbCr', 'CMYK']:
        raise ValueError(_errstr)

    if mode in ['RGB', 'YCbCr']:
        if numch != 3:
            raise ValueError("Invalid array shape for mode.")
    if mode in ['RGBA', 'CMYK']:
        if numch != 4:
            raise ValueError("Invalid array shape for mode.")

    # Here we know data and mode is correct
    image = Image.frombytes(mode, shape, strdata)
    return image

Example 29

Project: TensorflowProjects
Source File: Faces_GAN.py
View license
def main(argv=None):
    print("Setting up image reader...")
    train_images, valid_images, test_images = celebA.read_dataset(FLAGS.data_dir)
    image_options = {"crop": True, "crop_size": MODEL_IMAGE_SIZE, "resize": True, "resize_size": IMAGE_SIZE}
    dataset_reader = dataset.BatchDatset(train_images, image_options)

    # filename_queue = tf.train.string_input_producer(train_images)
    # images = read_input_queue(filename_queue)

    images = tf.placeholder(tf.float32, [FLAGS.batch_size, IMAGE_SIZE, IMAGE_SIZE, NUM_OF_CHANNELS])
    train_phase = tf.placeholder(tf.bool)
    z_vec = tf.placeholder(tf.float32, [FLAGS.batch_size, FLAGS.z_dim], name="z")

    print("Setting up network model...")
    tf.histogram_summary("z", z_vec)
    tf.image_summary("image_real", images, max_images=2)
    gen_images = generator(z_vec, train_phase)
    tf.image_summary("image_generated", gen_images, max_images=2)

    with tf.variable_scope("discriminator") as scope:
        discriminator_real_prob, logits_real, feature_real = discriminator(images, train_phase)
        utils.add_activation_summary(tf.identity(discriminator_real_prob, name='disc_real_prob'))
        scope.reuse_variables()
        discriminator_fake_prob, logits_fake, feature_fake = discriminator(gen_images, train_phase)
        utils.add_activation_summary(tf.identity(discriminator_fake_prob, name='disc_fake_prob'))

    discriminator_loss_real = tf.reduce_mean(
        tf.nn.sigmoid_cross_entropy_with_logits(logits_real, tf.ones_like(logits_real)))
    discrimintator_loss_fake = tf.reduce_mean(
        tf.nn.sigmoid_cross_entropy_with_logits(logits_fake, tf.zeros_like(logits_fake)))
    discriminator_loss = discrimintator_loss_fake + discriminator_loss_real
    gen_loss_1 = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits_fake, tf.ones_like(logits_fake)))
    gen_loss_2 = tf.reduce_mean(tf.nn.l2_loss(feature_real - feature_fake)) / (IMAGE_SIZE * IMAGE_SIZE)
    gen_loss = gen_loss_1 + 0.1 * gen_loss_2
    
    tf.scalar_summary("Discriminator_loss_real", discriminator_loss_real)
    tf.scalar_summary("Discrimintator_loss_fake", discrimintator_loss_fake)
    tf.scalar_summary("Discriminator_loss", discriminator_loss)
    tf.scalar_summary("Generator_loss", gen_loss)

    train_variables = tf.trainable_variables()
    generator_variables = [v for v in train_variables if v.name.startswith("generator")]
    # print(map(lambda x: x.op.name, generator_variables))
    discriminator_variables = [v for v in train_variables if v.name.startswith("discriminator")]
    # print(map(lambda x: x.op.name, discriminator_variables))
    generator_train_op = train(gen_loss, generator_variables)
    discriminator_train_op = train(discriminator_loss, discriminator_variables)

    for v in train_variables:
        utils.add_to_regularization_and_summary(var=v)

    sess = tf.Session()
    summary_op = tf.merge_all_summaries()
    saver = tf.train.Saver()
    summary_writer = tf.train.SummaryWriter(FLAGS.logs_dir, sess.graph)

    sess.run(tf.initialize_all_variables())
    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")
    # coord = tf.train.Coordinator()
    # threads = tf.train.start_queue_runners(sess, coord)
    try:
        for itr in xrange(MAX_ITERATIONS):
            batch_z = np.random.uniform(-1.0, 1.0, size=[FLAGS.batch_size, FLAGS.z_dim]).astype(np.float32)
            feed_dict = {images: dataset_reader.next_batch(FLAGS.batch_size), z_vec: batch_z, train_phase: True}

            # if itr % 2 == 0:
            #     sess.run(discriminator_train_op, feed_dict=feed_dict)
            sess.run(discriminator_train_op, feed_dict=feed_dict)
            sess.run(generator_train_op, feed_dict=feed_dict)
            sess.run(generator_train_op, feed_dict=feed_dict)
            # sess.run(generator_train_op, feed_dict={z_vec: batch_z, train_phase: True})

            if itr % 10 == 0:
                g_loss_val, d_loss_val, summary_str = sess.run([gen_loss, discriminator_loss, summary_op],
                                                               feed_dict=feed_dict)
                print("Step: %d, generator loss: %g, discriminator_loss: %g" % (itr, g_loss_val, d_loss_val))
                summary_writer.add_summary(summary_str, itr)

            if itr % 500 == 0:
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", global_step=itr)

    # except tf.errors.OutOfRangeError:
    #     print('Done training -- epoch limit reached')
    # finally:
    #     coord.request_stop()
    #
    # # Wait for threads to finish.
    # coord.join(threads)
    except KeyboardInterrupt:
        print("Ending Training...")

Example 30

View license
def main(_):
  if FLAGS.self_test:
    print('Running self-test.')
    train_data, train_labels = fake_data(256)
    validation_data, validation_labels = fake_data(EVAL_BATCH_SIZE)
    test_data, test_labels = fake_data(EVAL_BATCH_SIZE)
    num_epochs = 1
  else:
    # Get the data.
    train_data_filename = maybe_download('train-images-idx3-ubyte.gz')
    train_labels_filename = maybe_download('train-labels-idx1-ubyte.gz')
    test_data_filename = maybe_download('t10k-images-idx3-ubyte.gz')
    test_labels_filename = maybe_download('t10k-labels-idx1-ubyte.gz')

    # Extract it into numpy arrays.
    train_data = extract_data(train_data_filename, 60000)
    train_labels = extract_labels(train_labels_filename, 60000)
    test_data = extract_data(test_data_filename, 10000)
    test_labels = extract_labels(test_labels_filename, 10000)

    # Generate a validation set.
    validation_data = train_data[:VALIDATION_SIZE, ...]
    validation_labels = train_labels[:VALIDATION_SIZE]
    train_data = train_data[VALIDATION_SIZE:, ...]
    train_labels = train_labels[VALIDATION_SIZE:]
    num_epochs = NUM_EPOCHS
  train_size = train_labels.shape[0]

  # This is where training samples and labels are fed to the graph.
  # These placeholder nodes will be fed a batch of training data at each
  # training step using the {feed_dict} argument to the Run() call below.
  train_data_node = tf.placeholder(
      data_type(),
      shape=(BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS))
  train_labels_node = tf.placeholder(tf.int64, shape=(BATCH_SIZE,))
  eval_data = tf.placeholder(
      data_type(),
      shape=(EVAL_BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS))

  # The variables below hold all the trainable weights. They are passed an
  # initial value which will be assigned when we call:
  # {tf.global_variables_initializer().run()}
  conv1_weights = tf.Variable(
      tf.truncated_normal([5, 5, NUM_CHANNELS, 32],  # 5x5 filter, depth 32.
                          stddev=0.1,
                          seed=SEED, dtype=data_type()))
  conv1_biases = tf.Variable(tf.zeros([32], dtype=data_type()))
  conv2_weights = tf.Variable(tf.truncated_normal(
      [5, 5, 32, 64], stddev=0.1,
      seed=SEED, dtype=data_type()))
  conv2_biases = tf.Variable(tf.constant(0.1, shape=[64], dtype=data_type()))
  fc1_weights = tf.Variable(  # fully connected, depth 512.
      tf.truncated_normal([IMAGE_SIZE // 4 * IMAGE_SIZE // 4 * 64, 512],
                          stddev=0.1,
                          seed=SEED,
                          dtype=data_type()))
  fc1_biases = tf.Variable(tf.constant(0.1, shape=[512], dtype=data_type()))
  fc2_weights = tf.Variable(tf.truncated_normal([512, NUM_LABELS],
                                                stddev=0.1,
                                                seed=SEED,
                                                dtype=data_type()))
  fc2_biases = tf.Variable(tf.constant(
      0.1, shape=[NUM_LABELS], dtype=data_type()))

  # We will replicate the model structure for the training subgraph, as well
  # as the evaluation subgraphs, while sharing the trainable parameters.
  def model(data, train=False):
    """The Model definition."""
    # 2D convolution, with 'SAME' padding (i.e. the output feature map has
    # the same size as the input). Note that {strides} is a 4D array whose
    # shape matches the data layout: [image index, y, x, depth].
    conv = tf.nn.conv2d(data,
                        conv1_weights,
                        strides=[1, 1, 1, 1],
                        padding='SAME')
    # Bias and rectified linear non-linearity.
    relu = tf.nn.relu(tf.nn.bias_add(conv, conv1_biases))
    # Max pooling. The kernel size spec {ksize} also follows the layout of
    # the data. Here we have a pooling window of 2, and a stride of 2.
    pool = tf.nn.max_pool(relu,
                          ksize=[1, 2, 2, 1],
                          strides=[1, 2, 2, 1],
                          padding='SAME')
    conv = tf.nn.conv2d(pool,
                        conv2_weights,
                        strides=[1, 1, 1, 1],
                        padding='SAME')
    relu = tf.nn.relu(tf.nn.bias_add(conv, conv2_biases))
    pool = tf.nn.max_pool(relu,
                          ksize=[1, 2, 2, 1],
                          strides=[1, 2, 2, 1],
                          padding='SAME')
    # Reshape the feature map cuboid into a 2D matrix to feed it to the
    # fully connected layers.
    pool_shape = pool.get_shape().as_list()
    reshape = tf.reshape(
        pool,
        [pool_shape[0], pool_shape[1] * pool_shape[2] * pool_shape[3]])
    # Fully connected layer. Note that the '+' operation automatically
    # broadcasts the biases.
    hidden = tf.nn.relu(tf.matmul(reshape, fc1_weights) + fc1_biases)
    # Add a 50% dropout during training only. Dropout also scales
    # activations such that no rescaling is needed at evaluation time.
    if train:
      hidden = tf.nn.dropout(hidden, 0.5, seed=SEED)
    return tf.matmul(hidden, fc2_weights) + fc2_biases

  # Training computation: logits + cross-entropy loss.
  logits = model(train_data_node, True)
  loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
      logits, train_labels_node))

  # L2 regularization for the fully connected parameters.
  regularizers = (tf.nn.l2_loss(fc1_weights) + tf.nn.l2_loss(fc1_biases) +
                  tf.nn.l2_loss(fc2_weights) + tf.nn.l2_loss(fc2_biases))
  # Add the regularization term to the loss.
  loss += 5e-4 * regularizers

  # Optimizer: set up a variable that's incremented once per batch and
  # controls the learning rate decay.
  batch = tf.Variable(0, dtype=data_type())
  # Decay once per epoch, using an exponential schedule starting at 0.01.
  learning_rate = tf.train.exponential_decay(
      0.01,                # Base learning rate.
      batch * BATCH_SIZE,  # Current index into the dataset.
      train_size,          # Decay step.
      0.95,                # Decay rate.
      staircase=True)
  # Use simple momentum for the optimization.
  optimizer = tf.train.MomentumOptimizer(learning_rate,
                                         0.9).minimize(loss,
                                                       global_step=batch)

  # Predictions for the current training minibatch.
  train_prediction = tf.nn.softmax(logits)

  # Predictions for the test and validation, which we'll compute less often.
  eval_prediction = tf.nn.softmax(model(eval_data))

  # Small utility function to evaluate a dataset by feeding batches of data to
  # {eval_data} and pulling the results from {eval_predictions}.
  # Saves memory and enables this to run on smaller GPUs.
  def eval_in_batches(data, sess):
    """Get all predictions for a dataset by running it in small batches."""
    size = data.shape[0]
    if size < EVAL_BATCH_SIZE:
      raise ValueError("batch size for evals larger than dataset: %d" % size)
    predictions = numpy.ndarray(shape=(size, NUM_LABELS), dtype=numpy.float32)
    for begin in xrange(0, size, EVAL_BATCH_SIZE):
      end = begin + EVAL_BATCH_SIZE
      if end <= size:
        predictions[begin:end, :] = sess.run(
            eval_prediction,
            feed_dict={eval_data: data[begin:end, ...]})
      else:
        batch_predictions = sess.run(
            eval_prediction,
            feed_dict={eval_data: data[-EVAL_BATCH_SIZE:, ...]})
        predictions[begin:, :] = batch_predictions[begin - size:, :]
    return predictions

  # Create a local session to run the training.
  start_time = time.time()
  with tf.Session() as sess:
    # Run all the initializers to prepare the trainable parameters.
    tf.global_variables_initializer().run()
    print('Initialized!')
    # Loop through training steps.
    for step in xrange(int(num_epochs * train_size) // BATCH_SIZE):
      # Compute the offset of the current minibatch in the data.
      # Note that we could use better randomization across epochs.
      offset = (step * BATCH_SIZE) % (train_size - BATCH_SIZE)
      batch_data = train_data[offset:(offset + BATCH_SIZE), ...]
      batch_labels = train_labels[offset:(offset + BATCH_SIZE)]
      # This dictionary maps the batch data (as a numpy array) to the
      # node in the graph it should be fed to.
      feed_dict = {train_data_node: batch_data,
                   train_labels_node: batch_labels}
      # Run the optimizer to update weights.
      sess.run(optimizer, feed_dict=feed_dict)
      # print some extra information once reach the evaluation frequency
      if step % EVAL_FREQUENCY == 0:
        # fetch some extra nodes' data
        l, lr, predictions = sess.run([loss, learning_rate, train_prediction],
                                      feed_dict=feed_dict)
        elapsed_time = time.time() - start_time
        start_time = time.time()
        print('Step %d (epoch %.2f), %.1f ms' %
              (step, float(step) * BATCH_SIZE / train_size,
               1000 * elapsed_time / EVAL_FREQUENCY))
        print('Minibatch loss: %.3f, learning rate: %.6f' % (l, lr))
        print('Minibatch error: %.1f%%' % error_rate(predictions, batch_labels))
        print('Validation error: %.1f%%' % error_rate(
            eval_in_batches(validation_data, sess), validation_labels))
        sys.stdout.flush()
    # Finally print the result!
    test_error = error_rate(eval_in_batches(test_data, sess), test_labels)
    print('Test error: %.1f%%' % test_error)
    if FLAGS.self_test:
      print('test_error', test_error)
      assert test_error == 0.0, 'expected 0.0 test_error, got %.2f' % (
          test_error,)

Example 31

Project: spinn
Source File: stack.py
View license
    def make_backprop_scan(self, error_signal,
                           extra_cost_inputs=None,
                           compute_embedding_gradients=True):
        """
        Compile the backpropagation graph for a ThinStack instance.

        This method analyzes the feedforward graph of a ThinStack instance and
        analytically calculates its gradient with respect to the stack's
        parameters of interest.

        This method must be called by the client after constructing ThinStack.
        After creating the gradient graph, the outputs of the graph (gradient
        batches) are installed into an instance member self.gradients. This
        object is a dictionary mapping from stack shared variables (parameters)
        to their symbolic gradients.

        Args:
            error_signal: The external gradient d(cost)/d(stack top). A Theano
                batch of size `batch_size * model_dim`.
            extra_cost_inputs: Other symbolic variables which may be involved
                in the symbolic cost expression / the error signal that should
                be passed as extra inputs to a backpropagation scan.
            compute_embedding_gradients: Calculate gradients for each embedding
                vector.
        """

        # The meat of the implementation of this method is in the inner
        # function step_b. What comes before is mainly preparatory code; what
        # comes after is the scan invocation that unrolls the step_b function.

        assert hasattr(self, "stack_2_ptrs"), \
            ("self._make_scan (forward pass) must be defined before "
             "self.make_backprop_scan is called")

        # We need to add extra updates to the `_zero_updates` member, so we
        # must be called before `_zero_updates` is read.
        assert self._zero is None, \
            ("Can only install backprop on a fresh ThinStack. Don't call "
             "ThinStack.zero before setting up backprop.")

        if (compute_embedding_gradients
            and self._embedding_projection_network not in [None, util.IdentityLayer]):
            raise ValueError(
                "Do not support backprop for both an embedding projection "
                "layer and individual embeddings.")

        if self.use_input_batch_norm:
            raise ValueError(
                "Thin-stack backprop not supported with input batch-norm. Jon "
                "worked on BN gradients for 3 days without success, and then "
                "dropped it.")

        if extra_cost_inputs is None:
            extra_cost_inputs = []

        # Analytically calculate backpropagation graphs for an abstract single
        # timestep. The list 'wrt' contains all variables for which we should
        # collect gradients (this list of course depends on the nature of the
        # forward-prop activation function). The latter 3 returns are actually
        # graph "thunks" which return a concrete backpropagation graph when
        # provided with actual symbolic inputs / error signals.
        #
        # These graphs will be repeatedly invoked and chained together in the
        # code further below.
        wrt, f_proj_delta, f_shift_delta, f_reduce_delta = \
                self._make_backward_graphs()
        wrt_shapes = [wrt_i.get_value().shape for wrt_i in wrt]

        # Build shared variables for accumulating wrt deltas.
        wrt_vars = [theano.shared(np.zeros(wrt_shape, dtype=np.float32),
                                  name=self._prefix + "bwd/wrt/%s" % wrt_i)
                    for wrt_i, wrt_shape in zip(wrt, wrt_shapes)]
        # All of these need to be zeroed out in between batches.
        self._zero_updates += wrt_vars

        # Also accumulate embedding gradients separately
        if compute_embedding_gradients:
            dE = theano.shared(np.zeros(self.embeddings.get_value().shape,
                                        dtype=np.float32),
                               name=self._prefix + "bwd/wrt/embeddings")
            self._zero_updates.append(dE)
        else:
            # Make dE a dummy variable.
            dE = T.zeros((1,))

        # Useful batch zero-constants.
        zero_stack = T.zeros((self.batch_size, self.model_dim))
        zero_extra_inps = [T.zeros((self.batch_size, extra_shape[-1]))
                           for extra_shape in self.recurrence.extra_outputs]
        # Zero Jacobian matrices for masked reductions during backprop. May not
        # be used.
        zero_jac_wrts = [T.zeros((self.batch_size,) + wrt_shape)
                         for wrt_shape in wrt_shapes]

        DUMMY = util.zeros_nobroadcast((1,))

        def lookup(t_f, stack_fwd, stack_2_ptrs_t, buffer_cur_t,
                  stack_bwd_t, extra_bwd):
            """Retrieve all relevant bwd inputs/outputs at time `t`."""

            grad_cursor = t_f * self.batch_size + self._stack_shift
            main_grad = cuda.AdvancedSubtensor1Floats("B_maingrad")(
                stack_bwd_t, grad_cursor)
            extra_grads = tuple([
                cuda.AdvancedSubtensor1Floats("B_extragrad_%i" % i)(
                    extra_bwd_i, grad_cursor)
                for i, extra_bwd_i in enumerate(extra_bwd)])

            # Find the timesteps of the two elements involved in the potential
            # reduce at this timestep.
            t_c1 = (t_f - 1.0) * self.batch_size + self._stack_shift
            t_c2 = stack_2_ptrs_t

            # Find the two elements involved in the potential reduce.
            c1 = cuda.AdvancedSubtensor1Floats("B_stack1")(stack_fwd, t_c1)
            c2 = cuda.AdvancedSubtensor1Floats("B_stack2")(stack_fwd, t_c2)

            buffer_top_t = cuda.AdvancedSubtensor1Floats("B_buffer_top")(
                self.buffer_t, buffer_cur_t + self._buffer_shift)

            # Retrieve extra inputs from auxiliary stack(s).
            extra_inps_t = tuple([
                cuda.AdvancedSubtensor1Floats("B_extra_inp_%i" % i)(
                    extra_inp_i, t_c1)
                for extra_inp_i in self.final_aux_stacks])

            inputs = (c1, c2, buffer_top_t) + extra_inps_t
            grads = (main_grad,) + extra_grads
            return t_c1, t_c2, inputs, grads

        def step_b(# sequences
                   t_f, transitions_t_f, stack_2_ptrs_t, buffer_cur_t,
                   # accumulators
                   dE,
                   # rest (incl. outputs_info, non_sequences)
                   *rest):

            # Separate the accum arguments from the non-sequence arguments.
            n_wrt = len(wrt_shapes)
            n_extra_bwd = len(self.recurrence.extra_outputs)
            wrt_deltas = rest[:n_wrt]
            stack_bwd_t = rest[n_wrt]
            extra_bwd = rest[n_wrt + 1:n_wrt + 1 + n_extra_bwd]
            id_buffer, stack_final = \
                rest[n_wrt + 1 + n_extra_bwd:n_wrt + 1 + n_extra_bwd + 2]

            # At first iteration, drop the external error signal into the main
            # backward stack.
            stack_bwd_next = ifelse(T.eq(t_f, self.seq_length),
                                    T.set_subtensor(stack_bwd_t[-self.batch_size:], error_signal),
                                    stack_bwd_t)


            # Retrieve all relevant inputs/outputs at this timestep.
            t_c1, t_c2, inputs, grads = \
                lookup(t_f, stack_final, stack_2_ptrs_t, buffer_cur_t,
                       stack_bwd_next, extra_bwd)
            main_grad = grads[0]

            # Calculate deltas for this timestep.
            r_delta_inp, r_delta_wrt = f_reduce_delta(inputs, grads)
            # NB: main_grad is not passed to shift function.
            s_delta_inp, s_delta_wrt = f_shift_delta(inputs, grads[1:])

            # Check that delta function outputs match (at least in number).
            assert len(r_delta_inp) == len(s_delta_inp), \
                "%i %i" % (len(r_delta_inp), len(s_delta_inp))
            assert len(r_delta_wrt) == len(s_delta_wrt), \
                "%i %i" % (len(r_delta_wrt), len(s_delta_wrt))
            assert len(r_delta_inp) == 3 + len(self.aux_stacks), \
                "%i %i" % (len(r_delta_inp), 3 + len(self.aux_stacks))
            assert len(r_delta_wrt) == len(wrt)

            # Retrieve embedding indices on buffer at this timestep.
            # (Necessary for sending embedding gradients.)
            buffer_ids_t = cuda.AdvancedSubtensor1Floats("B_buffer_ids")(
                    id_buffer, buffer_cur_t + self._buffer_shift)

            # Prepare masks for op-wise gradient accumulation.
            # TODO: Record actual transitions (e.g. for model 1S and higher)
            # and repeat those here
            mask = transitions_t_f
            masks = [mask, mask.dimshuffle(0, "x"),
                     mask.dimshuffle(0, "x", "x")]

            # Accumulate gradients from the embedding projection network into
            # the stack op gradients.
            if f_proj_delta is not None:
                # Look up raw buffer top for this timestep -- i.e., buffer top
                # *before* the op at this timestep was performed. This was the
                # input to the projection network at this timestep.
                proj_input = cuda.AdvancedSubtensor1Floats("B_raw_buffer_top")(
                    self._raw_buffer_t, buffer_cur_t + self._buffer_shift)

                proj_inputs = (proj_input,)
                if self.use_input_dropout:
                    embedding_dropout_mask = cuda.AdvancedSubtensor1Floats("B_buffer_dropout")(
                        self._embedding_dropout_masks, buffer_cur_t + self._buffer_shift)
                    proj_inputs = (proj_input, embedding_dropout_mask)

                # Compute separate graphs based on gradient from above.
                # NB: We discard the delta_inp return here. The delta_inp
                # should actually be passed back to the raw embedding
                # parameters, but we don't have any reason to support this in
                # practice. (Either we backprop to embeddings or project them
                # and learn the projection -- not both.)
                if r_delta_inp[2] is not None:
                    _, m_proj_delta_wrt = f_proj_delta(proj_inputs,
                                                       (r_delta_inp[2],))
                    r_delta_wrt = util.merge_update_lists(r_delta_wrt, m_proj_delta_wrt)

                # If we shifted (moved the buffer top onto the stack), the
                # gradient from above is a combination of the accumulated stack
                # gradient (main_grad) and any buffer top deltas from the shift
                # function (e.g. tracking LSTM gradient).
                embedding_grad = main_grad
                if s_delta_inp[2] is not None:
                    embedding_grad += s_delta_inp[2]
                _, p_proj_delta_wrt = f_proj_delta(proj_inputs,
                                                   (embedding_grad,))
                s_delta_wrt = util.merge_update_lists(s_delta_wrt, p_proj_delta_wrt)

            ###########
            # STEP 1. #
            ###########
            # Accumulate deltas onto the graph inputs, switching over
            # shift/reduce decisions.
            stacks = (stack_bwd_next, stack_bwd_next,
                      (compute_embedding_gradients and dE) or None)
            cursors = (t_c1, t_c2,
                       (compute_embedding_gradients and buffer_ids_t) or None)
            # Handle potential aux bwd stacks.
            stacks += extra_bwd
            cursors += ((t_c1,)) * len(extra_bwd)
            new_stacks = {}
            for stack, cursor, r_delta, s_delta in zip(stacks, cursors, r_delta_inp, s_delta_inp):
                if stack is None or cursor is None:
                    continue
                elif r_delta is None and s_delta is None:
                    # Disconnected gradient.
                    continue

                base = new_stacks.get(stack, stack)
                mask_i = masks[(r_delta or s_delta).ndim - 1]
                if r_delta is None:
                    delta = (1. - mask_i) * s_delta
                elif s_delta is None:
                    delta = mask_i * r_delta
                else:
                    delta = mask_i * r_delta + (1. - mask_i) * s_delta

                # Run subtensor update on associated structure using the
                # current cursor.
                new_stack = cuda.AdvancedIncSubtensor1Floats(inplace=True)(
                    base, delta, cursor)
                new_stacks[stack] = new_stack

            ###########
            # STEP 2. #
            ###########
            # Accumulate deltas with respect to the variables in the wrt store,
            # again switching over shift/reduce decision.
            new_wrt_deltas = {}
            wrt_data = enumerate(zip(wrt, zero_jac_wrts, wrt_deltas,
                                     r_delta_wrt, s_delta_wrt))
            for i, (wrt_var, wrt_zero, accum_delta, r_delta, s_delta) in wrt_data:
                if r_delta is None and s_delta is None:
                    # Disconnected gradient.
                    continue

                # Check that tensors returned by delta functions match shape
                # expectations.
                assert r_delta is None or accum_delta.ndim == r_delta.ndim - 1, \
                    "%s %i %i" % (wrt_var.name, accum_delta.ndim, r_delta.ndim)
                assert s_delta is None or accum_delta.ndim == s_delta.ndim - 1, \
                    "%s %i %i" % (wrt_var.name, accum_delta.ndim, s_delta.ndim)

                mask_i = masks[(r_delta or s_delta).ndim - 1]
                if r_delta is None:
                    delta = T.switch(mask_i, wrt_zero, s_delta)
                elif s_delta is None:
                    delta = T.switch(mask_i, r_delta, wrt_zero)
                else:
                    delta = T.switch(mask_i, r_delta, s_delta)
                # TODO: Is this at all efficient? (Bring back GPURowSwitch?)
                delta = delta.sum(axis=0)
                # TODO: we want this to be inplace
                new_wrt_deltas[accum_delta] = accum_delta + delta

            # On shift ops, backprop the stack_bwd error onto the embedding
            # projection network / embedding parameters.
            # TODO make sparse?
            if compute_embedding_gradients:
                new_stacks[dE] = cuda.AdvancedIncSubtensor1Floats(inplace=True)(
                    new_stacks.get(dE, dE), (1. - masks[1]) * main_grad, buffer_ids_t)

            updates = dict(new_wrt_deltas.items() + new_stacks.items())
            updates = util.prepare_updates_dict(updates)

            return updates

        # TODO: These should come from forward pass -- not fixed -- in model
        # 1S, etc.
        transitions_f = T.cast(self.transitions.dimshuffle(1, 0),
                               dtype=theano.config.floatX)

        ts_f = T.cast(T.arange(1, self.seq_length + 1), dtype=theano.config.floatX)

        # Representation of buffer using embedding indices rather than values
        id_buffer = T.cast(self.X.flatten(), theano.config.floatX)
        # Build sequence of buffer pointers, where buf_ptrs[i] indicates the
        # buffer pointer values *before* computation at timestep *i* proceeds.
        # (This means we need to slice off the last actual buf_ptr output and
        # prepend a dummy.)
        buf_ptrs = T.concatenate([T.zeros((1, self.batch_size,)),
                                  self.buf_ptrs[:-1]], axis=0)

        sequences = [ts_f, transitions_f, self.stack_2_ptrs, buf_ptrs]
        outputs_info = []

        # Shared variables: Accumulated wrt deltas and bwd stacks.
        non_sequences = [dE] + wrt_vars
        non_sequences += [self.stack_bwd] + self.aux_bwd_stacks
        # More auxiliary data
        non_sequences += [id_buffer, self.final_stack]

        # More helpers (not referenced directly in code, but we need to include
        # them as non-sequences to satisfy scan strict mode)
        aux_data = [self.stack, self.buffer_t] + self.aux_stacks + self.final_aux_stacks
        aux_data += [self.X, self.transitions, self._raw_buffer_t]
        if self.use_input_dropout:
            aux_data.append(self._embedding_dropout_masks)
        aux_data += self._vs.vars.values() + extra_cost_inputs
        non_sequences += list(set(aux_data))

        bscan_ret, self.bscan_updates = theano.scan(
                step_b, sequences, outputs_info, non_sequences,
                go_backwards=True,
                n_steps=self.seq_length,
#                strict=True,
                name=self._prefix + "stack_bwd")

        self.gradients = {wrt_i: self.bscan_updates.get(wrt_var)
                          for wrt_i, wrt_var in zip(wrt, wrt_vars)}
        if compute_embedding_gradients:
            self.embedding_gradients = self.bscan_updates[dE]

Example 32

Project: iris
Source File: test_concatenate.py
View license
def _make_cube_3d(x, y, z, data, aux=None, offset=0):
    """
    A convenience test function that creates a custom 3D cube.

    Args:

    * x:
        A (start, stop, step) tuple for specifying the
        x-axis dimensional coordinate points. Bounds are
        automatically guessed.

    * y:
        A (start, stop, step) tuple for specifying the
        y-axis dimensional coordinate points. Bounds are
        automatically guessed.

    * z:
        A (start, stop, step) tuple for specifying the
        z-axis dimensional coordinate points. Bounds are
        automatically guessed.

    * data:
        The data payload for the cube.

    Kwargs:

    * aux:
        A CSV string specifying which points only auxiliary
        coordinates to create. Accepts either of 'x', 'y', 'z',
        'xy', 'xz', 'yz', 'xyz'.

    * offset:
        Offset value to be added to non-1D auxiliary coordinate
        points.

    Returns:
        The newly created 3D :class:`iris.cube.Cube`.

    """
    x_range = np.arange(*x, dtype=np.float32)
    y_range = np.arange(*y, dtype=np.float32)
    z_range = np.arange(*z, dtype=np.float32)
    x_size, y_size, z_size = len(x_range), len(y_range), len(z_range)

    cube_data = np.empty((x_size, y_size, z_size), dtype=np.float32)
    cube_data[:] = data
    cube = iris.cube.Cube(cube_data)
    coord = DimCoord(z_range, long_name='z')
    coord.guess_bounds()
    cube.add_dim_coord(coord, 0)
    coord = DimCoord(y_range, long_name='y')
    coord.guess_bounds()
    cube.add_dim_coord(coord, 1)
    coord = DimCoord(x_range, long_name='x')
    coord.guess_bounds()
    cube.add_dim_coord(coord, 2)

    if aux is not None:
        aux = aux.split(',')
        if 'z' in aux:
            coord = AuxCoord(z_range * 10, long_name='z-aux')
            cube.add_aux_coord(coord, (0,))
        if 'y' in aux:
            coord = AuxCoord(y_range * 10, long_name='y-aux')
            cube.add_aux_coord(coord, (1,))
        if 'x' in aux:
            coord = AuxCoord(x_range * 10, long_name='x-aux')
            cube.add_aux_coord(coord, (2,))
        if 'xy' in aux:
            payload = np.arange(x_size * y_size,
                                dtype=np.float32).reshape(y_size, x_size)
            coord = AuxCoord(payload + offset, long_name='xy-aux')
            cube.add_aux_coord(coord, (1, 2))
        if 'xz' in aux:
            payload = np.arange(x_size * z_size,
                                dtype=np.float32).reshape(z_size, x_size)
            coord = AuxCoord(payload * 10 + offset, long_name='xz-aux')
            cube.add_aux_coord(coord, (0, 2))
        if 'yz' in aux:
            payload = np.arange(y_size * z_size,
                                dtype=np.float32).reshape(z_size, y_size)
            coord = AuxCoord(payload * 100 + offset, long_name='yz-aux')
            cube.add_aux_coord(coord, (0, 1))
        if 'xyz' in aux:
            payload = np.arange(x_size * y_size * z_size,
                                dtype=np.float32).reshape(z_size,
                                                          y_size,
                                                          x_size)
            coord = AuxCoord(payload * 1000 + offset, long_name='xyz-aux')
            cube.add_aux_coord(coord, (0, 1, 2))

    return cube

Example 33

Project: kaggle_diabetic
Source File: blend.py
View license
@click.command()
@click.option('--cnf', default='configs/c_512_4x4_32.py', show_default=True,
              help="Path or name of configuration module.")
@click.option('--predict', is_flag=True, default=False, show_default=True,
              help="Make predictions on test set features after training.")
@click.option('--per_patient', is_flag=True, default=False, show_default=True,
              help="Blend features of both patient eyes.")
@click.option('--features_file', default=None, show_default=True,
              help="Read features from specified file.")
@click.option('--n_iter', default=1, show_default=True,
              help="Number of times to fit and average.")
@click.option('--blend_cnf', default='blend.yml', show_default=True,
              help="Blending configuration file.")
@click.option('--test_dir', default=None, show_default=True,
              help="Override directory with test set images.")
def fit(cnf, predict, per_patient, features_file, n_iter, blend_cnf, test_dir):

    config = util.load_module(cnf).config
    image_files = data.get_image_files(config.get('train_dir'))
    names = data.get_names(image_files)
    labels = data.get_labels(names).astype(np.float32)[:, np.newaxis]

    if features_file is not None:
        runs = {'run': [features_file]}
    else:
        runs = data.parse_blend_config(yaml.load(open(blend_cnf)))

    scalers = {run: StandardScaler() for run in runs}

    tr, te = data.split_indices(image_files, labels)

    y_preds = []
    for i in range(n_iter):
        print("iteration {} / {}".format(i + 1, n_iter))
        for run, files in runs.items():
            print("fitting features for run {}".format(run))
            X = data.load_features(files)
            X = scalers[run].fit_transform(X)
            X = data.per_patient_reshape(X) if per_patient else X
            est = get_estimator(X.shape[1], image_files, labels,
                                eval_size=0.0 if predict else 0.1)
            est.fit(X, labels)
            if not predict:
                y_pred = est.predict(X[te]).ravel()
                y_preds.append(y_pred)
                y_pred = np.mean(y_preds, axis=0)
                y_pred = np.clip(np.round(y_pred).astype(int),
                                 np.min(labels), np.max(labels))
                print("kappa after run {}, iter {}: {}".format(
                    run, i, util.kappa(labels[te], y_pred)))
                print("confusion matrix")
                print(confusion_matrix(labels[te], y_pred))
            else:
                X = data.load_features(files, test=True)
                X = scalers[run].transform(X)
                X = data.per_patient_reshape(X) if per_patient else X
                y_pred = est.predict(X).ravel()
                y_preds.append(y_pred)

    if predict:
        y_pred = np.mean(y_preds, axis=0)
        y_pred = np.clip(np.round(y_pred),
                         np.min(labels), np.max(labels)).astype(int)
        submission_filename = util.get_submission_filename()
        image_files = data.get_image_files(test_dir or config.get('test_dir'))
        names = data.get_names(image_files)
        image_column = pd.Series(names, name='image')
        level_column = pd.Series(y_pred, name='level')
        predictions = pd.concat([image_column, level_column], axis=1)

        print("tail of predictions file")
        print(predictions.tail())

        predictions.to_csv(submission_filename, index=False)
        print("saved predictions to {}".format(submission_filename))

Example 34

Project: pycollada
Source File: controller.py
View license
    @staticmethod
    def load( collada, localscope, skinnode, controllernode ):
        if len(localscope) < 3:
            raise DaeMalformedError('Not enough sources in skin')

        geometry_source = skinnode.get('source')
        if geometry_source is None or len(geometry_source) < 2 \
                or geometry_source[0] != '#':
            raise DaeBrokenRefError('Invalid source attribute of skin node')
        if not geometry_source[1:] in collada.geometries:
            raise DaeBrokenRefError('Source geometry for skin node not found')
        geometry = collada.geometries[geometry_source[1:]]

        bind_shape_mat = skinnode.find(tag('bind_shape_matrix'))
        if bind_shape_mat is None:
            bind_shape_mat = numpy.identity(4, dtype=numpy.float32)
            bind_shape_mat.shape = (-1,)
        else:
            try:
                values = [ float(v) for v in bind_shape_mat.text.split()]
            except ValueError:
                raise DaeMalformedError('Corrupted bind shape matrix in skin')
            bind_shape_mat = numpy.array( values, dtype=numpy.float32 )

        inputnodes = skinnode.findall('%s/%s'%(tag('joints'), tag('input')))
        if inputnodes is None or len(inputnodes) < 2:
            raise DaeIncompleteError("Not enough inputs in skin joints")

        try:
            inputs = [(i.get('semantic'), i.get('source')) for i in inputnodes]
        except ValueError as ex:
            raise DaeMalformedError('Corrupted inputs in skin')

        joint_source = None
        matrix_source = None
        for i in inputs:
            if len(i[1]) < 2 or i[1][0] != '#':
                raise DaeBrokenRefError('Input in skin node %s not found'%i[1])
            if i[0] == 'JOINT':
                joint_source = i[1][1:]
            elif i[0] == 'INV_BIND_MATRIX':
                matrix_source = i[1][1:]

        weightsnode = skinnode.find(tag('vertex_weights'))
        if weightsnode is None:
            raise DaeIncompleteError("No vertex_weights found in skin")
        indexnode = weightsnode.find(tag('v'))
        if indexnode is None:
            raise DaeIncompleteError('Missing indices in skin vertex weights')
        vcountnode = weightsnode.find(tag('vcount'))
        if vcountnode is None:
            raise DaeIncompleteError('Missing vcount in skin vertex weights')
        inputnodes = weightsnode.findall(tag('input'))

        try:
            index = numpy.array([float(v)
                for v in indexnode.text.split()], dtype=numpy.int32)
            vcounts = numpy.array([int(v)
                for v in vcountnode.text.split()], dtype=numpy.int32)
            inputs = [(i.get('semantic'), i.get('source'), int(i.get('offset')))
                           for i in inputnodes]
        except ValueError as ex:
            raise DaeMalformedError('Corrupted index or offsets in skin vertex weights')

        weight_joint_source = None
        weight_source = None
        offsets = [0, 0]
        for i in inputs:
            if len(i[1]) < 2 or i[1][0] != '#':
                raise DaeBrokenRefError('Input in skin node %s not found' % i[1])
            if i[0] == 'JOINT':
                weight_joint_source = i[1][1:]
                offsets[0] = i[2]
            elif i[0] == 'WEIGHT':
                weight_source = i[1][1:]
                offsets[1] = i[2]

        if joint_source is None or weight_source is None:
            raise DaeMalformedError('Not enough inputs for vertex weights in skin')

        return Skin(localscope, bind_shape_mat, joint_source, matrix_source,
                weight_source, weight_joint_source, vcounts, index, offsets,
                geometry, controllernode, skinnode)

Example 35

Project: fits2hdf
Source File: benchmark_compression.py
View license
def load_fits(file_name):
    """ Load FITS file, create various things """

    output_dir_fits = 'fits_generated'
    output_dir_hdf  = 'hdf_generated'
    _mkdir(output_dir_fits)
    _mkdir(output_dir_hdf)

    idi_img = fitsio.read_fits(file_name)

    for hdu_name in idi_img:
        hdu = idi_img[hdu_name]
        if isinstance(hdu, idi.IdiTableHdu):
            for col in hdu.colnames:
                if hdu[col].dtype.type is np.float32:
                    #print "CONVERTING %s TO INT" % col
                    hdu[col] = hdu[col].astype('int32')
                    if col == 'FLUX':
                        print("FRUX")
                        hdu[col] = hdu[col].data / 16
                    hdu[col].dtype = 'int32'
                    #print hdu[col].dtype

    img_name = os.path.split(file_name)[1]
    img_name = os.path.splitext(img_name)[0]
    name = img_name
    #idi_img.add_image_hdu(img_name, data=data)

    # Create all the filenames
    fits_filename = join(output_dir_fits, img_name+'.fits')
    hdf_filename = join(output_dir_hdf, img_name+'.h5')
    hdf_comp_filename = join(output_dir_hdf, img_name+'_comp.h5')
    gzip_comp_filename = join(output_dir_fits, img_name+'.fits.gz')
    fits_comp_filename = join(output_dir_fits, img_name+'.fits.fz')

    # Delete files that already exists
    file_list = [fits_filename, hdf_filename, fits_comp_filename,
                 hdf_comp_filename, gzip_comp_filename]
    for fname in file_list:
        if exists(fname):
            os.remove(fname)


    print("\nWriting {} to \n{}\n{}".format(img_name,fits_filename,hdf_filename))
    t1 = time.time()
    fitsio.export_fits(idi_img, fits_filename)
    t2 = time.time()

    hdfio.export_hdf(idi_img, hdf_filename)
    t3 = time.time()

    hdfio.export_hdf(idi_img, hdf_comp_filename, **hdf_opts)
    t4 = time.time()

    subprocess.check_call(['fpack','-table',fits_filename])
    t5 = time.time()

    subprocess.check_call(['gzip','-k',fits_filename])
    t6 = time.time()

    dd = {
        'img_name': name,
        'fits_size': getsize(fits_filename),
        'hdf_size':  getsize(hdf_filename),
        'hdf_comp_size':  getsize(hdf_comp_filename),
        'fits_comp_size': getsize(fits_filename + '.fz'),
        'gzip_comp_size': getsize(fits_filename + '.gz'),
        'fits_time': (t2 - t1),
        'hdf_time': (t3 - t2),
        'hdf_comp_time': (t4 - t3),
        'fits_comp_time': (t5 - t4),
        'gzip_comp_time': (t6 - t5),
        'comp_fact_hdf': float(getsize(fits_filename)) / getsize(hdf_comp_filename),
        'comp_fact_fits': float(getsize(fits_filename)) / getsize(fits_comp_filename),
        'comp_fact_gzip': float(getsize(fits_filename)) / getsize(gzip_comp_filename)
    }

    rh = dd['comp_fact_gzip']
    th = dd['gzip_comp_time']

    dd["weissman_hdf"] = weissman_score(dd["comp_fact_hdf"], dd["hdf_comp_time"], rh, th)
    dd["weissman_fits"] = weissman_score(dd["comp_fact_fits"], dd["fits_comp_time"], rh, th)

    print("FITS file size:        %sB" % dd['fits_size'])
    print("HDF file size:         %sB" % dd['hdf_size'])
    print("FITS comp size:        %sB" % dd['fits_comp_size'])
    print("HDF comp size:         %sB" % dd['hdf_comp_size'])
    print("GZIP comp size:        %sB" % dd['gzip_comp_size'])
    print("FITS creation time:    %2.2fs" % dd['fits_time'])
    print("HDF  creation time:    %2.2fs" % dd['hdf_time'])
    print("FITS comp time:        %2.2fs" % dd['fits_comp_time'])
    print("HDF  comp time:        %2.2fs" % dd['hdf_comp_time'])
    print("GZIP comp time:        %2.2fs" % dd['gzip_comp_time'])
    print("FITS/FITS compression: %2.2fx" % dd['comp_fact_fits'])
    print("HDF/FITS compression:  %2.2fx" % dd['comp_fact_hdf'])
    print("GZIP/FITS compression: %2.2fx" % dd['comp_fact_gzip'])
    print("FITS weissman score:   %2.2f" % dd['weissman_fits'])
    print("HDF  weissman score:   %2.2f" % dd['weissman_hdf'])


    return dd

Example 36

View license
def main(_):
  if FLAGS.self_test:
    print('Running self-test.')
    train_data, train_labels = fake_data(256)
    validation_data, validation_labels = fake_data(EVAL_BATCH_SIZE)
    test_data, test_labels = fake_data(EVAL_BATCH_SIZE)
    num_epochs = 1
  else:
    # Get the data.
    train_data_filename = maybe_download('train-images-idx3-ubyte.gz')
    train_labels_filename = maybe_download('train-labels-idx1-ubyte.gz')
    test_data_filename = maybe_download('t10k-images-idx3-ubyte.gz')
    test_labels_filename = maybe_download('t10k-labels-idx1-ubyte.gz')

    # Extract it into numpy arrays.
    train_data = extract_data(train_data_filename, 60000)
    train_labels = extract_labels(train_labels_filename, 60000)
    test_data = extract_data(test_data_filename, 10000)
    test_labels = extract_labels(test_labels_filename, 10000)

    # Generate a validation set.
    validation_data = train_data[:VALIDATION_SIZE, ...]
    validation_labels = train_labels[:VALIDATION_SIZE]
    train_data = train_data[VALIDATION_SIZE:, ...]
    train_labels = train_labels[VALIDATION_SIZE:]
    num_epochs = NUM_EPOCHS
  train_size = train_labels.shape[0]

  # This is where training samples and labels are fed to the graph.
  # These placeholder nodes will be fed a batch of training data at each
  # training step using the {feed_dict} argument to the Run() call below.
  train_data_node = tf.placeholder(
      data_type(),
      shape=(BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS))
  train_labels_node = tf.placeholder(tf.int64, shape=(BATCH_SIZE,))
  eval_data = tf.placeholder(
      data_type(),
      shape=(EVAL_BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, NUM_CHANNELS))

  # The variables below hold all the trainable weights. They are passed an
  # initial value which will be assigned when we call:
  # {tf.global_variables_initializer().run()}
  conv1_weights = tf.Variable(
      tf.truncated_normal([5, 5, NUM_CHANNELS, 32],  # 5x5 filter, depth 32.
                          stddev=0.1,
                          seed=SEED, dtype=data_type()))
  conv1_biases = tf.Variable(tf.zeros([32], dtype=data_type()))
  conv2_weights = tf.Variable(tf.truncated_normal(
      [5, 5, 32, 64], stddev=0.1,
      seed=SEED, dtype=data_type()))
  conv2_biases = tf.Variable(tf.constant(0.1, shape=[64], dtype=data_type()))
  fc1_weights = tf.Variable(  # fully connected, depth 512.
      tf.truncated_normal([IMAGE_SIZE // 4 * IMAGE_SIZE // 4 * 64, 512],
                          stddev=0.1,
                          seed=SEED,
                          dtype=data_type()))
  fc1_biases = tf.Variable(tf.constant(0.1, shape=[512], dtype=data_type()))
  fc2_weights = tf.Variable(tf.truncated_normal([512, NUM_LABELS],
                                                stddev=0.1,
                                                seed=SEED,
                                                dtype=data_type()))
  fc2_biases = tf.Variable(tf.constant(
      0.1, shape=[NUM_LABELS], dtype=data_type()))

  # We will replicate the model structure for the training subgraph, as well
  # as the evaluation subgraphs, while sharing the trainable parameters.
  def model(data, train=False):
    """The Model definition."""
    # 2D convolution, with 'SAME' padding (i.e. the output feature map has
    # the same size as the input). Note that {strides} is a 4D array whose
    # shape matches the data layout: [image index, y, x, depth].
    conv = tf.nn.conv2d(data,
                        conv1_weights,
                        strides=[1, 1, 1, 1],
                        padding='SAME')
    # Bias and rectified linear non-linearity.
    relu = tf.nn.relu(tf.nn.bias_add(conv, conv1_biases))
    # Max pooling. The kernel size spec {ksize} also follows the layout of
    # the data. Here we have a pooling window of 2, and a stride of 2.
    pool = tf.nn.max_pool(relu,
                          ksize=[1, 2, 2, 1],
                          strides=[1, 2, 2, 1],
                          padding='SAME')
    conv = tf.nn.conv2d(pool,
                        conv2_weights,
                        strides=[1, 1, 1, 1],
                        padding='SAME')
    relu = tf.nn.relu(tf.nn.bias_add(conv, conv2_biases))
    pool = tf.nn.max_pool(relu,
                          ksize=[1, 2, 2, 1],
                          strides=[1, 2, 2, 1],
                          padding='SAME')
    # Reshape the feature map cuboid into a 2D matrix to feed it to the
    # fully connected layers.
    pool_shape = pool.get_shape().as_list()
    reshape = tf.reshape(
        pool,
        [pool_shape[0], pool_shape[1] * pool_shape[2] * pool_shape[3]])
    # Fully connected layer. Note that the '+' operation automatically
    # broadcasts the biases.
    hidden = tf.nn.relu(tf.matmul(reshape, fc1_weights) + fc1_biases)
    # Add a 50% dropout during training only. Dropout also scales
    # activations such that no rescaling is needed at evaluation time.
    if train:
      hidden = tf.nn.dropout(hidden, 0.5, seed=SEED)
    return tf.matmul(hidden, fc2_weights) + fc2_biases

  # Training computation: logits + cross-entropy loss.
  logits = model(train_data_node, True)
  loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
      logits, train_labels_node))

  # L2 regularization for the fully connected parameters.
  regularizers = (tf.nn.l2_loss(fc1_weights) + tf.nn.l2_loss(fc1_biases) +
                  tf.nn.l2_loss(fc2_weights) + tf.nn.l2_loss(fc2_biases))
  # Add the regularization term to the loss.
  loss += 5e-4 * regularizers

  # Optimizer: set up a variable that's incremented once per batch and
  # controls the learning rate decay.
  batch = tf.Variable(0, dtype=data_type())
  # Decay once per epoch, using an exponential schedule starting at 0.01.
  learning_rate = tf.train.exponential_decay(
      0.01,                # Base learning rate.
      batch * BATCH_SIZE,  # Current index into the dataset.
      train_size,          # Decay step.
      0.95,                # Decay rate.
      staircase=True)
  # Use simple momentum for the optimization.
  optimizer = tf.train.MomentumOptimizer(learning_rate,
                                         0.9).minimize(loss,
                                                       global_step=batch)

  # Predictions for the current training minibatch.
  train_prediction = tf.nn.softmax(logits)

  # Predictions for the test and validation, which we'll compute less often.
  eval_prediction = tf.nn.softmax(model(eval_data))

  # Small utility function to evaluate a dataset by feeding batches of data to
  # {eval_data} and pulling the results from {eval_predictions}.
  # Saves memory and enables this to run on smaller GPUs.
  def eval_in_batches(data, sess):
    """Get all predictions for a dataset by running it in small batches."""
    size = data.shape[0]
    if size < EVAL_BATCH_SIZE:
      raise ValueError("batch size for evals larger than dataset: %d" % size)
    predictions = numpy.ndarray(shape=(size, NUM_LABELS), dtype=numpy.float32)
    for begin in xrange(0, size, EVAL_BATCH_SIZE):
      end = begin + EVAL_BATCH_SIZE
      if end <= size:
        predictions[begin:end, :] = sess.run(
            eval_prediction,
            feed_dict={eval_data: data[begin:end, ...]})
      else:
        batch_predictions = sess.run(
            eval_prediction,
            feed_dict={eval_data: data[-EVAL_BATCH_SIZE:, ...]})
        predictions[begin:, :] = batch_predictions[begin - size:, :]
    return predictions

  # Create a local session to run the training.
  start_time = time.time()
  with tf.Session() as sess:
    # Run all the initializers to prepare the trainable parameters.
    tf.global_variables_initializer().run()
    print('Initialized!')
    # Loop through training steps.
    for step in xrange(int(num_epochs * train_size) // BATCH_SIZE):
      # Compute the offset of the current minibatch in the data.
      # Note that we could use better randomization across epochs.
      offset = (step * BATCH_SIZE) % (train_size - BATCH_SIZE)
      batch_data = train_data[offset:(offset + BATCH_SIZE), ...]
      batch_labels = train_labels[offset:(offset + BATCH_SIZE)]
      # This dictionary maps the batch data (as a numpy array) to the
      # node in the graph it should be fed to.
      feed_dict = {train_data_node: batch_data,
                   train_labels_node: batch_labels}
      # Run the optimizer to update weights.
      sess.run(optimizer, feed_dict=feed_dict)
      # print some extra information once reach the evaluation frequency
      if step % EVAL_FREQUENCY == 0:
        # fetch some extra nodes' data
        l, lr, predictions = sess.run([loss, learning_rate, train_prediction],
                                      feed_dict=feed_dict)
        elapsed_time = time.time() - start_time
        start_time = time.time()
        print('Step %d (epoch %.2f), %.1f ms' %
              (step, float(step) * BATCH_SIZE / train_size,
               1000 * elapsed_time / EVAL_FREQUENCY))
        print('Minibatch loss: %.3f, learning rate: %.6f' % (l, lr))
        print('Minibatch error: %.1f%%' % error_rate(predictions, batch_labels))
        print('Validation error: %.1f%%' % error_rate(
            eval_in_batches(validation_data, sess), validation_labels))
        sys.stdout.flush()
    # Finally print the result!
    test_error = error_rate(eval_in_batches(test_data, sess), test_labels)
    print('Test error: %.1f%%' % test_error)
    if FLAGS.self_test:
      print('test_error', test_error)
      assert test_error == 0.0, 'expected 0.0 test_error, got %.2f' % (
          test_error,)

Example 37

Project: models
Source File: deep_cnn.py
View license
def train(images, labels, ckpt_path, dropout=False):
  """
  This function contains the loop that actually trains the model.
  :param images: a numpy array with the input data
  :param labels: a numpy array with the output labels
  :param ckpt_path: a path (including name) where model checkpoints are saved
  :param dropout: Boolean, whether to use dropout or not
  :return: True if everything went well
  """

  # Check training data
  assert len(images) == len(labels)
  assert images.dtype == np.float32
  assert labels.dtype == np.int32

  # Set default TF graph
  with tf.Graph().as_default():
    global_step = tf.Variable(0, trainable=False)

    # Declare data placeholder
    train_data_node = _input_placeholder()

    # Create a placeholder to hold labels
    train_labels_shape = (FLAGS.batch_size,)
    train_labels_node = tf.placeholder(tf.int32, shape=train_labels_shape)

    print("Done Initializing Training Placeholders")

    # Build a Graph that computes the logits predictions from the placeholder
    if FLAGS.deeper:
      logits = inference_deeper(train_data_node, dropout=dropout)
    else:
      logits = inference(train_data_node, dropout=dropout)

    # Calculate loss
    loss = loss_fun(logits, train_labels_node)

    # Build a Graph that trains the model with one batch of examples and
    # updates the model parameters.
    train_op = train_op_fun(loss, global_step)

    # Create a saver.
    saver = tf.train.Saver(tf.all_variables())

    print("Graph constructed and saver created")

    # Build an initialization operation to run below.
    init = tf.initialize_all_variables()

    # Create and init sessions
    sess = tf.Session(config=tf.ConfigProto(log_device_placement=FLAGS.log_device_placement)) #NOLINT(long-line)
    sess.run(init)

    print("Session ready, beginning training loop")

    # Initialize the number of batches
    data_length = len(images)
    nb_batches = math.ceil(data_length / FLAGS.batch_size)

    for step in xrange(FLAGS.max_steps):
      # for debug, save start time
      start_time = time.time()

      # Current batch number
      batch_nb = step % nb_batches

      # Current batch start and end indices
      start, end = utils.batch_indices(batch_nb, data_length, FLAGS.batch_size)

      # Prepare dictionnary to feed the session with
      feed_dict = {train_data_node: images[start:end],
                   train_labels_node: labels[start:end]}

      # Run training step
      _, loss_value = sess.run([train_op, loss], feed_dict=feed_dict)

      # Compute duration of training step
      duration = time.time() - start_time

      # Sanity check
      assert not np.isnan(loss_value), 'Model diverged with loss = NaN'

      # Echo loss once in a while
      if step % 100 == 0:
        num_examples_per_step = FLAGS.batch_size
        examples_per_sec = num_examples_per_step / duration
        sec_per_batch = float(duration)

        format_str = ('%s: step %d, loss = %.2f (%.1f examples/sec; %.3f '
                      'sec/batch)')
        print (format_str % (datetime.now(), step, loss_value,
                             examples_per_sec, sec_per_batch))

      # Save the model checkpoint periodically.
      if step % 1000 == 0 or (step + 1) == FLAGS.max_steps:
        saver.save(sess, ckpt_path, global_step=step)

  return True

Example 38

Project: galry
Source File: sprite_visual.py
View license
    def initialize(self, x=None, y=None, color=None, autocolor=None,
            texture=None, position=None, point_size=None, zoomable=False):
            
        # if position is specified, it contains x and y as column vectors
        if position is not None:
            position = np.array(position, dtype=np.float32)
            # shape = (position.shape[0], 1)
        else:
            position, shape = process_coordinates(x=x, y=y)
            
        texsize = float(max(texture.shape[:2]))
        shape = texture.shape
        ncomponents = texture.shape[2]
        self.size = position.shape[0]
        
        if shape[0] == 1:
            self.ndim = 1
        elif shape[0] > 1:
            self.ndim = 2
        
        self.primitive_type = 'POINTS'
        
        # normalize position
        # if viewbox:
            # self.add_normalizer('position', viewbox)
        # self.normalize = normalize
            
        # default color
        if color is None:
            color = self.default_color
        
        
        # automatic color with color map
        if autocolor is not None:
            color = get_next_color(autocolor)
            
        
        color = get_color(color)
        
        # handle the case where there is a single color given as a list of
        # RGB components instead of a tuple
        if type(color) is list:
            if color and (type(color[0]) != tuple) and (3 <= len(color) <= 4):
                color = tuple(color)
            else:
                color = np.array(color)
        if isinstance(color, np.ndarray):
            colors_ndim = color.shape[1]
            # one color per point
            single_color = False
        elif type(color) is tuple:
            single_color = True
            colors_ndim = len(color)
            
            
        texture_shader = """
        out_color = texture%NDIM%(tex_sampler, gl_PointCoord%POINTCOORD%) * %COLOR%;
        """
            
        
        shader_ndim = "%dD" % self.ndim
        if self.ndim == 1:
            shader_pointcoord = ".x"
        else:
            shader_pointcoord = ""
            
        # single color case: no need for a color buffer, just use default color
        if single_color:
            self.add_uniform("color", ndim=colors_ndim, data=color)   
            shader_color_name = "color"
        # multiple colors case: color attribute
        else:
            self.add_attribute("color", ndim=colors_ndim, data=color)
            self.add_varying("varying_color", vartype="float", ndim=colors_ndim)
            self.add_vertex_main("""
            varying_color = color;
            """)
            shader_color_name = "varying_color"
            
        if colors_ndim == 3:
            shader_color = "vec4(%s, 1.0)" % shader_color_name
        elif colors_ndim == 4:
            shader_color = shader_color_name
        
        texture_shader = texture_shader.replace('%COLOR%', shader_color)
        texture_shader = texture_shader.replace('%NDIM%', shader_ndim)
        texture_shader = texture_shader.replace('%POINTCOORD%', shader_pointcoord)
        self.add_fragment_main(texture_shader)
        
        # add variables
        self.add_attribute("position", vartype="float", ndim=2, data=position,
            autonormalizable=True)
        self.add_texture("tex_sampler", size=shape, ndim=self.ndim,
            ncomponents=ncomponents)
        self.add_compound("texture", fun=lambda texture: \
                         dict(tex_sampler=texture), data=texture)
        
        # size
        if point_size is None:
            point_size = texsize
        
        if isinstance(point_size, np.ndarray):
            self.add_attribute("point_size", vartype="float", ndim=1,
                data=point_size)
        else:
            self.add_uniform("point_size", vartype="float", ndim=1, data=point_size)
        
        # Vertex shader
        if zoomable:
            # The size of the points increases with zoom.
            self.add_vertex_main("""
            gl_PointSize = point_size * max(scale.x, scale.y);
            """)
        else:
            self.add_vertex_main("""
            gl_PointSize = point_size;
            """)

Example 39

View license
  def testBasic(self):
    """Test two batch entries."""
    # Input and ground truth from Alex Graves' implementation.
    #
    #### Batch entry 0 #####
    # targets: 0 1 2 1 0
    # outputs:
    # 0 0.633766 0.221185 0.0917319 0.0129757 0.0142857 0.0260553
    # 1 0.111121 0.588392 0.278779 0.0055756 0.00569609 0.010436
    # 2 0.0357786 0.633813 0.321418 0.00249248 0.00272882 0.0037688
    # 3 0.0663296 0.643849 0.280111 0.00283995 0.0035545 0.00331533
    # 4 0.458235 0.396634 0.123377 0.00648837 0.00903441 0.00623107
    # alpha:
    # 0 -3.64753 -0.456075 -inf -inf -inf -inf -inf -inf -inf -inf -inf
    # 1 -inf -inf -inf -0.986437 -inf -inf -inf -inf -inf -inf -inf
    # 2 -inf -inf -inf -inf -inf -2.12145 -inf -inf -inf -inf -inf
    # 3 -inf -inf -inf -inf -inf -inf -inf -2.56174 -inf -inf -inf
    # 4 -inf -inf -inf -inf -inf -inf -inf -inf -inf -3.34211 -inf
    # beta:
    # 0 -inf -2.88604 -inf -inf -inf -inf -inf -inf -inf -inf -inf
    # 1 -inf -inf -inf -2.35568 -inf -inf -inf -inf -inf -inf -inf
    # 2 -inf -inf -inf -inf -inf -1.22066 -inf -inf -inf -inf -inf
    # 3 -inf -inf -inf -inf -inf -inf -inf -0.780373 -inf -inf -inf
    # 4 -inf -inf -inf -inf -inf -inf -inf -inf -inf 0 0
    # prob: -3.34211
    # outputDerivs:
    # 0 -0.366234 0.221185 0.0917319 0.0129757 0.0142857 0.0260553
    # 1 0.111121 -0.411608 0.278779 0.0055756 0.00569609 0.010436
    # 2 0.0357786 0.633813 -0.678582 0.00249248 0.00272882 0.0037688
    # 3 0.0663296 -0.356151 0.280111 0.00283995 0.0035545 0.00331533
    # 4 -0.541765 0.396634 0.123377 0.00648837 0.00903441 0.00623107
    #
    #### Batch entry 1 #####
    #
    # targets: 0 1 1 0
    # outputs:
    # 0 0.30176 0.28562 0.0831517 0.0862751 0.0816851 0.161508
    # 1 0.24082 0.397533 0.0557226 0.0546814 0.0557528 0.19549
    # 2 0.230246 0.450868 0.0389607 0.038309 0.0391602 0.202456
    # 3 0.280884 0.429522 0.0326593 0.0339046 0.0326856 0.190345
    # 4 0.423286 0.315517 0.0338439 0.0393744 0.0339315 0.154046
    # alpha:
    # 0 -1.8232 -1.19812 -inf -inf -inf -inf -inf -inf -inf
    # 1 -inf -2.19315 -2.83037 -2.1206 -inf -inf -inf -inf -inf
    # 2 -inf -inf -inf -2.03268 -3.71783 -inf -inf -inf -inf
    # 3 -inf -inf -inf -inf -inf -4.56292 -inf -inf -inf
    # 4 -inf -inf -inf -inf -inf -inf -inf -5.42262 -inf
    # beta:
    # 0 -inf -4.2245 -inf -inf -inf -inf -inf -inf -inf
    # 1 -inf -inf -inf -3.30202 -inf -inf -inf -inf -inf
    # 2 -inf -inf -inf -inf -1.70479 -0.856738 -inf -inf -inf
    # 3 -inf -inf -inf -inf -inf -0.859706 -0.859706 -0.549337 -inf
    # 4 -inf -inf -inf -inf -inf -inf -inf 0 0
    # prob: -5.42262
    # outputDerivs:
    # 0 -0.69824 0.28562 0.0831517 0.0862751 0.0816851 0.161508
    # 1 0.24082 -0.602467 0.0557226 0.0546814 0.0557528 0.19549
    # 2 0.230246 0.450868 0.0389607 0.038309 0.0391602 -0.797544
    # 3 0.280884 -0.570478 0.0326593 0.0339046 0.0326856 0.190345
    # 4 -0.576714 0.315517 0.0338439 0.0393744 0.0339315 0.154046

    # max_time_steps == 7
    depth = 6

    # seq_len_0 == 5
    targets_0 = [0, 1, 2, 1, 0]
    loss_log_prob_0 = -3.34211
    # dimensions are time x depth
    input_prob_matrix_0 = np.asarray(
        [[0.633766, 0.221185, 0.0917319, 0.0129757, 0.0142857, 0.0260553],
         [0.111121, 0.588392, 0.278779, 0.0055756, 0.00569609, 0.010436],
         [0.0357786, 0.633813, 0.321418, 0.00249248, 0.00272882, 0.0037688],
         [0.0663296, 0.643849, 0.280111, 0.00283995, 0.0035545, 0.00331533],
         [0.458235, 0.396634, 0.123377, 0.00648837, 0.00903441, 0.00623107]],
        dtype=np.float32)
    input_log_prob_matrix_0 = np.log(input_prob_matrix_0)
    gradient_log_prob_0 = np.asarray(
        [[-0.366234, 0.221185, 0.0917319, 0.0129757, 0.0142857, 0.0260553],
         [0.111121, -0.411608, 0.278779, 0.0055756, 0.00569609, 0.010436],
         [0.0357786, 0.633813, -0.678582, 0.00249248, 0.00272882, 0.0037688],
         [0.0663296, -0.356151, 0.280111, 0.00283995, 0.0035545, 0.00331533],
         [-0.541765, 0.396634, 0.123377, 0.00648837, 0.00903441, 0.00623107]],
        dtype=np.float32)

    # seq_len_1 == 5
    targets_1 = [0, 1, 1, 0]
    loss_log_prob_1 = -5.42262
    # dimensions are time x depth

    input_prob_matrix_1 = np.asarray(
        [[0.30176, 0.28562, 0.0831517, 0.0862751, 0.0816851, 0.161508],
         [0.24082, 0.397533, 0.0557226, 0.0546814, 0.0557528, 0.19549],
         [0.230246, 0.450868, 0.0389607, 0.038309, 0.0391602, 0.202456],
         [0.280884, 0.429522, 0.0326593, 0.0339046, 0.0326856, 0.190345],
         [0.423286, 0.315517, 0.0338439, 0.0393744, 0.0339315, 0.154046]],
        dtype=np.float32)
    input_log_prob_matrix_1 = np.log(input_prob_matrix_1)
    gradient_log_prob_1 = np.asarray(
        [[-0.69824, 0.28562, 0.0831517, 0.0862751, 0.0816851, 0.161508],
         [0.24082, -0.602467, 0.0557226, 0.0546814, 0.0557528, 0.19549],
         [0.230246, 0.450868, 0.0389607, 0.038309, 0.0391602, -0.797544],
         [0.280884, -0.570478, 0.0326593, 0.0339046, 0.0326856, 0.190345],
         [-0.576714, 0.315517, 0.0338439, 0.0393744, 0.0339315, 0.154046]],
        dtype=np.float32)

    # len max_time_steps array of 2 x depth matrices
    inputs = [np.vstack([input_log_prob_matrix_0[t, :],
                         input_log_prob_matrix_1[t, :]])
              for t in range(5)] + 2 * [np.nan*np.ones((2, depth), np.float32)]

    # convert inputs into [max_time x batch_size x depth tensor] Tensor
    inputs = np.asarray(inputs, dtype=np.float32)

    # len batch_size array of label vectors
    labels = SimpleSparseTensorFrom([targets_0, targets_1])

    # batch_size length vector of sequence_lengths
    seq_lens = np.array([5, 5], dtype=np.int32)

    # output: batch_size length vector of negative log probabilities
    loss_truth = np.array([-loss_log_prob_0, -loss_log_prob_1], np.float32)

    # output: len max_time_steps array of 2 x depth matrices
    grad_truth = [np.vstack([gradient_log_prob_0[t, :],
                             gradient_log_prob_1[t, :]])
                  for t in range(5)] + 2 * [np.zeros((2, depth), np.float32)]

    # convert grad_truth into [max_time x batch_size x depth] Tensor
    grad_truth = np.asarray(grad_truth, dtype=np.float32)

    self._testCTCLoss(inputs, seq_lens, labels, loss_truth, grad_truth)

Example 40

Project: TensorflowProjects
Source File: Flowers_GAN.py
View license
def main(argv=None):
    print("Setting up image reader...")
    train_images, valid_images, test_images = flowers.read_dataset(FLAGS.data_dir)
    # image_options = {"crop": True, "crop_size": MODEL_IMAGE_SIZE, "resize": True, "resize_size": IMAGE_SIZE}
    # dataset_reader = dataset.BatchDatset(train_images, image_options)
    # images = tf.placeholder(tf.float32, [FLAGS.batch_size, IMAGE_SIZE, IMAGE_SIZE, NUM_OF_CHANNELS])
    filename_queue = tf.train.string_input_producer(train_images)
    images = read_input_queue(filename_queue)

    train_phase = tf.placeholder(tf.bool)
    z_vec = tf.placeholder(tf.float32, [None, FLAGS.z_dim], name="z")

    print("Setting up network model...")
    tf.histogram_summary("z", z_vec)
    tf.image_summary("image_real", images, max_images=1)
    gen_images = generator(z_vec, train_phase)
    tf.image_summary("image_generated", gen_images, max_images=3)

    with tf.variable_scope("discriminator") as scope:
        discriminator_real_prob, logits_real, feature_real = discriminator(images, train_phase)
        utils.add_activation_summary(tf.identity(discriminator_real_prob, name='disc_real_prob'))
        scope.reuse_variables()
        discriminator_fake_prob, logits_fake, feature_fake = discriminator(gen_images, train_phase)
        utils.add_activation_summary(tf.identity(discriminator_fake_prob, name='disc_fake_prob'))

    discriminator_loss_real = tf.reduce_mean(
        tf.nn.sigmoid_cross_entropy_with_logits(logits_real, tf.ones_like(logits_real)))
    discrimintator_loss_fake = tf.reduce_mean(
        tf.nn.sigmoid_cross_entropy_with_logits(logits_fake, tf.zeros_like(logits_fake)))
    discriminator_loss = discrimintator_loss_fake + discriminator_loss_real
    gen_loss_1 = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits_fake, tf.ones_like(logits_fake)))
    gen_loss_2 = tf.reduce_mean(tf.nn.l2_loss(feature_real - feature_fake)) / (IMAGE_SIZE * IMAGE_SIZE)
    gen_loss = gen_loss_1 + 0.1 * gen_loss_2

    tf.scalar_summary("Discriminator_loss_real", discriminator_loss_real)
    tf.scalar_summary("Discrimintator_loss_fake", discrimintator_loss_fake)
    tf.scalar_summary("Discriminator_loss", discriminator_loss)
    tf.scalar_summary("Generator_loss", gen_loss)

    train_variables = tf.trainable_variables()
    generator_variables = [v for v in train_variables if v.name.startswith("generator")]
    # print(map(lambda x: x.op.name, generator_variables))
    discriminator_variables = [v for v in train_variables if v.name.startswith("discriminator")]
    # print(map(lambda x: x.op.name, discriminator_variables))
    generator_train_op = train(gen_loss, generator_variables)
    discriminator_train_op = train(discriminator_loss, discriminator_variables)

    for v in train_variables:
        utils.add_to_regularization_and_summary(var=v)

    def visualize():
        count = 20
        z_feed = np.random.uniform(-1.0, 1.0, size=(count, FLAGS.z_dim)).astype(np.float32)
        # z_feed = np.tile(np.random.uniform(-1.0, 1.0, size=(1, FLAGS.z_dim)).astype(np.float32), (count, 1))
        # z_feed[:, 25] = sorted(10.0 * np.random.randn(count))
        image = sess.run(gen_images, feed_dict={z_vec: z_feed, train_phase: False})

        for iii in xrange(count):
            print(image.shape)
            utils.save_image(image[iii, :, :, :], IMAGE_SIZE, FLAGS.logs_dir, name=str(iii))
            print("Saving image" + str(iii))

    sess = tf.Session()
    summary_op = tf.merge_all_summaries()
    saver = tf.train.Saver()
    summary_writer = tf.train.SummaryWriter(FLAGS.logs_dir, sess.graph)

    sess.run(tf.initialize_all_variables())
    ckpt = tf.train.get_checkpoint_state(FLAGS.logs_dir)
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
        print("Model restored...")
        visualize()
        return

    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess, coord)
    try:
        for itr in xrange(MAX_ITERATIONS):
            batch_z = np.random.uniform(-1.0, 1.0, size=[FLAGS.batch_size, FLAGS.z_dim]).astype(np.float32)
            # feed_dict = {images: dataset_reader.next_batch(FLAGS.batch_size), z_vec: batch_z, train_phase: True}
            feed_dict = {z_vec: batch_z, train_phase: True}

            sess.run(discriminator_train_op, feed_dict=feed_dict)
            sess.run(generator_train_op, feed_dict=feed_dict)
            sess.run(generator_train_op, feed_dict=feed_dict)

            if itr % 10 == 0:
                g_loss_val, d_loss_val, summary_str = sess.run([gen_loss, discriminator_loss, summary_op],
                                                               feed_dict=feed_dict)
                print("Step: %d, generator loss: %g, discriminator_loss: %g" % (itr, g_loss_val, d_loss_val))
                summary_writer.add_summary(summary_str, itr)

            if itr % 500 == 0:
                saver.save(sess, FLAGS.logs_dir + "model.ckpt", global_step=itr)

    except tf.errors.OutOfRangeError:
        print('Done training -- epoch limit reached')
    except KeyboardInterrupt:
        print("Ending Training...")
    finally:
        coord.request_stop()

    # Wait for threads to finish.
    coord.join(threads)

Example 41

Project: cupy
Source File: n_step_lstm.py
View license
    def forward(self, inputs):
        (hx, cx), inputs = _split(inputs, 2)
        ws, inputs = _split(inputs, self.n_layers * 8)
        bs, inputs = _split(inputs, self.n_layers * 8)
        x_list = inputs

        hx = cuda.cupy.ascontiguousarray(hx)
        cx = cuda.cupy.ascontiguousarray(cx)

        x_desc = cudnn.create_tensor_nd_descriptor(x_list[0][..., None])

        length = len(x_list)
        n_units = hx.shape[2]

        xs = cuda.cupy.concatenate(x_list, axis=0)
        ys = cuda.cupy.empty((len(xs), n_units), dtype=xs.dtype)

        handle = cudnn.get_handle()
        self.handle = handle

        rnn_desc = cudnn.create_rnn_descriptor(
            n_units, self.n_layers, self.states.desc,
            libcudnn.CUDNN_LINEAR_INPUT, libcudnn.CUDNN_UNIDIRECTIONAL,
            libcudnn.CUDNN_LSTM, libcudnn.CUDNN_DATA_FLOAT)
        self.rnn_desc = rnn_desc

        c_x_descs = _make_tensor_descriptor_array(x_list)
        hx_desc = cudnn.create_tensor_nd_descriptor(hx)
        cx_desc = cudnn.create_tensor_nd_descriptor(cx)

        weights_size = libcudnn.getRNNParamsSize(
            handle, rnn_desc.value, x_desc.value, libcudnn.CUDNN_DATA_FLOAT)
        w = cuda.cupy.empty((weights_size // 4, 1, 1), dtype=numpy.float32)
        w_desc = cudnn.create_filter_descriptor(w)

        for layer in six.moves.range(self.n_layers):
            for lin_layer_id in six.moves.range(8):
                mat = cudnn.get_rnn_lin_layer_matrix_params(
                    handle, rnn_desc, layer, x_desc, w_desc, w,
                    lin_layer_id)
                m = mat.reshape(mat.size)
                m[...] = ws[layer * 8 + lin_layer_id].ravel()
                bias = cudnn.get_rnn_lin_layer_bias_params(
                    handle, rnn_desc, layer, x_desc, w_desc, w,
                    lin_layer_id)
                b = bias.reshape(bias.size)
                b[...] = bs[layer * 8 + lin_layer_id]
        self.w = w
        self.w_desc = w_desc

        sections = numpy.cumsum([len(x) for x in x_list[:-1]])
        y_list = cuda.cupy.split(ys, sections)

        c_y_descs = _make_tensor_descriptor_array(y_list)
        hy = cuda.cupy.empty_like(hx)
        cy = cuda.cupy.empty_like(cx)
        hy_desc = cudnn.create_tensor_nd_descriptor(hy)
        cy_desc = cudnn.create_tensor_nd_descriptor(cy)

        work_size = libcudnn.getRNNWorkspaceSize(
            handle, rnn_desc.value, length, c_x_descs.data)
        workspace = cuda.cupy.empty((work_size,), dtype='b')
        self.workspace = workspace

        if not self.train:
            libcudnn.RNNForwardInference(
                handle, rnn_desc.value, length,
                c_x_descs.data, xs.data.ptr, hx_desc.value, hx.data.ptr,
                cx_desc.value, cx.data.ptr, w_desc.value, w.data.ptr,
                c_y_descs.data, ys.data.ptr, hy_desc.value, hy.data.ptr,
                cy_desc.value, cy.data.ptr, workspace.data.ptr, work_size)

        else:
            reserve_size = libcudnn.getRNNTrainingReserveSize(
                handle, rnn_desc.value, length, c_x_descs.data)
            self.reserve_space = cuda.cupy.empty((reserve_size,), dtype='b')
            libcudnn.RNNForwardTraining(
                handle, rnn_desc.value, length,
                c_x_descs.data, xs.data.ptr, hx_desc.value, hx.data.ptr,
                cx_desc.value, cx.data.ptr, w_desc.value, w.data.ptr,
                c_y_descs.data, ys.data.ptr, hy_desc.value, hy.data.ptr,
                cy_desc.value, cy.data.ptr,
                workspace.data.ptr, work_size,
                self.reserve_space.data.ptr, reserve_size)

        self.c_y_descs = c_y_descs
        self.ys = ys
        self.c_x_descs = c_x_descs

        return tuple([hy, cy] + y_list)

Example 42

Project: numexpr
Source File: test_numexpr.py
View license
def test_expressions():
    test_no = [0]

    def make_test_method(a, a2, b, c, d, e, x, expr,
                         test_scalar, dtype, optimization, exact, section):
        this_locals = locals()

        def method():
            # We don't want to listen at RuntimeWarnings like
            # "overflows" or "divide by zero" in plain eval().
            warnings.simplefilter("ignore")
            npval = eval(expr, globals(), this_locals)
            warnings.simplefilter("always")
            npval = eval(expr, globals(), this_locals)
            try:
                neval = evaluate(expr, local_dict=this_locals,
                                 optimization=optimization)
                assert equal(npval, neval, exact), """%r
(test_scalar=%r, dtype=%r, optimization=%r, exact=%r,
 npval=%r (%r - %r)\n neval=%r (%r - %r))""" % (expr, test_scalar, dtype.__name__,
                                                optimization, exact,
                                                npval, type(npval), shape(npval),
                                                neval, type(neval), shape(neval))
            except AssertionError:
                raise
            except NotImplementedError:
                print('%r not implemented for %s (scalar=%d, opt=%s)'
                      % (expr, dtype.__name__, test_scalar, optimization))
            except:
                print('numexpr error for expression %r' % (expr,))
                raise

        method.description = ('test_expressions(%s, test_scalar=%r, '
                              'dtype=%r, optimization=%r, exact=%r)') \
                             % (expr, test_scalar, dtype.__name__, optimization, exact)
        test_no[0] += 1
        method.__name__ = 'test_scalar%d_%s_%s_%s_%04d' % (test_scalar,
                                                           dtype.__name__,
                                                           optimization.encode('ascii'),
                                                           section.encode('ascii'),
                                                           test_no[0])
        return method

    x = None
    for test_scalar in (0, 1, 2):
        for dtype in (int, long, np.float32, double, complex):
            array_size = 100
            a = arange(2 * array_size, dtype=dtype)[::2]
            a2 = zeros([array_size, array_size], dtype=dtype)
            b = arange(array_size, dtype=dtype) / array_size
            c = arange(array_size, dtype=dtype)
            d = arange(array_size, dtype=dtype)
            e = arange(array_size, dtype=dtype)
            if dtype == complex:
                a = a.real
                for x in [a2, b, c, d, e]:
                    x += 1j
                    x *= 1 + 1j
            if test_scalar == 1:
                a = a[array_size // 2]
            if test_scalar == 2:
                b = b[array_size // 2]
            for optimization, exact in [
                ('none', False), ('moderate', False), ('aggressive', False)]:
                for section_name, section_tests in tests:
                    for expr in section_tests:
                        if (dtype == complex and
                            ('<' in expr or '>' in expr or '%' in expr
                             or "arctan2" in expr or "fmod" in expr)):
                            # skip complex comparisons or functions not
                            # defined in complex domain.
                            continue
                        if (dtype in (int, long) and test_scalar and
                                    expr == '(a+1) ** -1'):
                            continue

                        m = make_test_method(a, a2, b, c, d, e, x,
                                             expr, test_scalar, dtype,
                                             optimization, exact,
                                             section_name)
                        yield m

Example 43

Project: Emotion-Recognition-RNN
Source File: trainer.py
View license
    def __init__(self, model=None, inputs=None, outputs=None, masks=None,
                 batchsize=100, learningrate=.01,
                 momentum=0.9, loadsize=None,
                 rng=None, verbose=True,
                 gradient_clip_threshold=1000,
                 numepochs_per_load=1,
                 rmsprop=False, cost=None, params=None, inputvar=None,
                 grads=None, monitor_update_weight_norm_ratio=False,
                 auto_reset_on_naninf=True,
                 monitor_by_epoch_not_load=False,
                 targetvar=None, maskvar=None, preprocess_fn=None):

        self.verbose = verbose
        self.monitor_by_epoch_not_load = monitor_by_epoch_not_load
        if not hasattr(preprocess_fn, '__call__'):
            preprocess_fn = lambda x: x
        self.preprocess_fn = preprocess_fn


#{{{ Initialization of Properties
        assert model is not None or (
            cost is not None and params is not None and
            inputvar is not None and grads is not None
        ), (
            "either a model instance or cost, params and inputvar "
            "have to be passed to the SGD_Trainer constructor")

        self.auto_reset_on_naninf = auto_reset_on_naninf
        self.monitor_update_weight_norm_ratio = monitor_update_weight_norm_ratio
        print 'monitor_update_weight_norm_ratio: {0}'.format(monitor_update_weight_norm_ratio, )

        self.is_supervised = False
        self.has_masks = False

        self._inputs_type = 'numpy'
        self._inputs = inputs
        if outputs is not None:
            self._outputs = outputs
            self.is_supervised = True
        if masks is not None:
            self.has_masks = True
            self._masks = masks

        if model is not None:
            self._model = model
            self._params = model.params
            self._cost = model._cost
            self._inputvar = model.inputs
            self._grads = model._grads
            if self.is_supervised:
                try:
                    self._targetvar = model.targets
                except AttributeError:
                    print 'Training in supervised mode, but model does not have target variable'
                    raise
            if self.has_masks:
                try:
                    self._maskvar = model.masks
                except AttributeError:
                    print 'Training in masked cost mode, but model does not have mask variable'
                    raise
        else:
            self._params = params
            self._cost = cost
            self._inputvar = inputvar
            self._grads = grads
            if self.is_supervised:
                try:
                    self._targetvar = targetvar
                except AttributeError:
                    print 'Training in supervised mode, but no target variable specified'
                    raise
            if self.has_masks:
                try:
                    self._maskvar = maskvar
                except AttributeError:
                    print 'Training in masked cost mode, but no mask variable specified'
                    raise

        # compute total number of params
        self._nparams = 0
        for p in self._params:
            try:
                self._nparams += p.get_value().size
            except AttributeError:
                # handles scalar params
                self._nparams += 1
        print 'number of params: {0}'.format(self._nparams)

        if monitor_update_weight_norm_ratio:
            self._update_weight_norm_ratios_log = dict(
                [(p, []) for p in self._params])

        self._learningrate = theano.shared(np.float32(learningrate),
                                           name='learningrate')
        self.numepochs_per_load = numepochs_per_load

        self._momentum = theano.shared(np.float32(momentum),
                                       name='momentum')
        self._total_stepcount = 0

        self._gradient_clip_threshold = theano.shared(
            np.float32(gradient_clip_threshold),
            name='gradient_clip_threshold')
        self._avg_gradnorm = theano.shared(np.float32(0.), name='avg_gradnorm')

        self._model = model

        self._numparams = reduce(lambda x, y: x + y,
                                 [p.get_value().size for p in self._params])

        self.numcases = self._inputs.shape[0]

        self.batchsize = batchsize
        self.loadsize = loadsize
        if self.batchsize > self.loadsize:
            warnings.warn('batchsize > loadsize -> batchsize := loadsize')
            self.batchsize = self.loadsize

        if self.loadsize % self.batchsize:
            self.loadsize = int(
                self.loadsize // self.batchsize + 1) * self.batchsize

        if rng is None:
            self._rng = np.random.RandomState(1)
        else:
            self._rng = rng

        # prepare loads
        self.prepare_loads()

        # use first load to allocate shared variable
        self._inputs_theano = theano.shared(
            self._inputs[self.data_indices[0]],
            name='inputs')
        if self.is_supervised:
            self._outputs_theano = theano.shared(
                self._outputs[self.data_indices[0]],
                name='outputs')
        if self.has_masks:
            self._masks_theano = theano.shared(
                self._masks[self.data_indices[0]],
                name='masks')

        self._momentum_batchcounter = 0

        self._epochcount = 0
        self._index = T.lscalar()
        self._incs = \
            dict([(p, theano.shared(
                value=np.zeros(p.get_value().shape,
                               dtype=theano.config.floatX),
                name='inc_' + p.name))
                for p in self._params])
        self._inc_updates = collections.OrderedDict()
        self.rmsprop = rmsprop
        if self.rmsprop:
            self.averaging_coeff = 0.95
            self.stabilizer = 1e-2
            self._avg_grad_sqrs = \
                dict([(p, theano.shared(value=np.zeros(
                    p.get_value().shape,
                    dtype=theano.config.floatX), name='avg_grad_sqr_' + p.name))
                    for p in self._params])
        self._avg_grad_sqrs_updates = collections.OrderedDict()
        self._updates_nomomentum = collections.OrderedDict()
        self._updates = collections.OrderedDict()
        self._n = T.lscalar('n')
        self._n.tag.test_value = 0.
        self._noop = 0.0 * self._n
        self._batch_idx = theano.shared(
            value=np.array(0, dtype=np.int64), name='batch_idx')

        self.costs = []
        self._compile_functions()

Example 44

View license
def main_test_layers(model='relu'):
    X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_mnist_dataset(shape=(-1,784))

    X_train = np.asarray(X_train, dtype=np.float32)
    y_train = np.asarray(y_train, dtype=np.int64)
    X_val = np.asarray(X_val, dtype=np.float32)
    y_val = np.asarray(y_val, dtype=np.int64)
    X_test = np.asarray(X_test, dtype=np.float32)
    y_test = np.asarray(y_test, dtype=np.int64)

    print('X_train.shape', X_train.shape)
    print('y_train.shape', y_train.shape)
    print('X_val.shape', X_val.shape)
    print('y_val.shape', y_val.shape)
    print('X_test.shape', X_test.shape)
    print('y_test.shape', y_test.shape)
    print('X %s   y %s' % (X_test.dtype, y_test.dtype))

    sess = tf.InteractiveSession()

    # placeholder
    x = tf.placeholder(tf.float32, shape=[None, 784], name='x')
    y_ = tf.placeholder(tf.int64, shape=[None, ], name='y_')


    if model == 'relu':
        with tf.variable_scope("model", reuse=None):
            network = tl.layers.InputLayer(x, name='input_layer')
            network = tl.layers.DropoutLayer(network, keep=0.8, name='drop1')
            network = tl.layers.DenseLayer(network, n_units=800, act = tf.nn.relu, name='relu1')
            network = tl.layers.DropoutLayer(network, keep=0.5, name='drop2')
            network = tl.layers.DenseLayer(network, n_units=800, act = tf.nn.relu, name='relu2')
            network = tl.layers.DropoutLayer(network, keep=0.5, name='drop3')
            network = tl.layers.DenseLayer(network, n_units=10, act = tl.activation.identity, name='output_layer')
        with tf.variable_scope("model", reuse=True):
            set_keep['_layers_name_list'] = []
            network2 = tl.layers.InputLayer(x, name='input_layer')
            network2 = tl.layers.DenseLayer(network2, n_units=800, act = tf.nn.relu, name='relu1')
            network2 = tl.layers.DenseLayer(network2, n_units=800, act = tf.nn.relu, name='relu2')
            network2 = tl.layers.DenseLayer(network2, n_units=10, act = tl.activation.identity, name='output_layer')
        print('a',network.all_params,len(network.all_params))
        print('b',network2.all_params,len(network2.all_params))
        print(network2.all_drop)
        exit()
    elif model == 'resnet':
        network = tl.layers.InputLayer(x, name='input_layer')
        network = tl.layers.DropoutLayer(network, keep=0.8, name='drop1')
        network = tl.layers.ResnetLayer(network, act = tf.nn.relu, name='resnet1')
        network = tl.layers.DropoutLayer(network, keep=0.5, name='drop2')
        network = tl.layers.ResnetLayer(network, act = tf.nn.relu, name='resnet2')
        network = tl.layers.DropoutLayer(network, keep=0.5, name='drop3')
        network = tl.layers.DenseLayer(network, act = tl.activation.identity, name='output_layer')
    elif model == 'dropconnect':
        network = tl.layers.InputLayer(x, name='input_layer')
        network = tl.layers.DropconnectDenseLayer(network, keep = 0.8, n_units=800, act = tf.nn.relu, name='dropconnect_relu1')
        network = tl.layers.DropconnectDenseLayer(network, keep = 0.5, n_units=800, act = tf.nn.relu, name='dropconnect_relu2')
        network = tl.layers.DropconnectDenseLayer(network, keep = 0.5, n_units=10, act = tl.activation.identity, name='output_layer')

    # attrs = vars(network)
    # print(', '.join("%s: %s\n" % item for item in attrs.items()))

    # print(network.all_drop)     # {'drop1': 0.8, 'drop2': 0.5, 'drop3': 0.5}
    # print(drop1, drop2, drop3)  # Tensor("Placeholder_2:0", dtype=float32) Tensor("Placeholder_3:0", dtype=float32) Tensor("Placeholder_4:0", dtype=float32)
    # exit()

    y = network.outputs
    y_op = tf.argmax(tf.nn.softmax(y), 1)
    cost = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(y, y_))

    # cost = cost + tl.cost.maxnorm_regularizer(1.0)(network.all_params[0]) + tl.cost.maxnorm_regularizer(1.0)(network.all_params[2])
    # cost = cost + tl.cost.lo_regularizer(0.0001)(network.all_params[0]) + tl.cost.lo_regularizer(0.0001)(network.all_params[2])
    # cost = cost + tl.cost.maxnorm_o_regularizer(0.001)(network.all_params[0]) + tl.cost.maxnorm_o_regularizer(0.001)(network.all_params[2])


    params = network.all_params
    # train
    n_epoch = 100
    batch_size = 128
    learning_rate = 0.0001
    print_freq = 10
    train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False).minimize(cost)

    sess.run(tf.initialize_all_variables()) # initialize all variables

    network.print_params()
    network.print_layers()

    print('   learning_rate: %f' % learning_rate)
    print('   batch_size: %d' % batch_size)

    for epoch in range(n_epoch):
        start_time = time.time()
        for X_train_a, y_train_a in tl.iterate.minibatches(X_train, y_train, batch_size, shuffle=True):
            feed_dict = {x: X_train_a, y_: y_train_a}
            feed_dict.update( network.all_drop )    # enable all dropout/dropconnect/denoising layers
            sess.run(train_op, feed_dict=feed_dict)

            # The optional feed_dict argument allows the caller to override the value of tensors in the graph. Each key in feed_dict can be one of the following types:
            # If the key is a Tensor, the value may be a Python scalar, string, list, or numpy ndarray that can be converted to the same dtype as that tensor. Additionally, if the key is a placeholder, the shape of the value will be checked for compatibility with the placeholder.
            # If the key is a SparseTensor, the value should be a SparseTensorValue.

        if epoch + 1 == 1 or (epoch + 1) % print_freq == 0:
            print("Epoch %d of %d took %fs" % (epoch + 1, n_epoch, time.time() - start_time))
            dp_dict = tl.utils.dict_to_one( network.all_drop ) # disable all dropout/dropconnect/denoising layers
            feed_dict = {x: X_train, y_: y_train}
            feed_dict.update(dp_dict)
            print("   train loss: %f" % sess.run(cost, feed_dict=feed_dict))
            dp_dict = tl.utils.dict_to_one( network.all_drop )
            feed_dict = {x: X_val, y_: y_val}
            feed_dict.update(dp_dict)
            print("   val loss: %f" % sess.run(cost, feed_dict=feed_dict))
            print("   val acc: %f" % np.mean(y_val == sess.run(y_op, feed_dict=feed_dict)))
            try:
                # You can visualize the weight of 1st hidden layer as follow.
                tl.visualize.W(network.all_params[0].eval(), second=10, saveable=True, shape=[28, 28], name='w1_'+str(epoch+1), fig_idx=2012)
                # You can also save the weight of 1st hidden layer to .npz file.
                # tl.files.save_npz([network.all_params[0]] , name='w1'+str(epoch+1)+'.npz')
            except:
                raise Exception("You should change visualize_W(), if you want to save the feature images for different dataset")

    print('Evaluation')
    dp_dict = tl.utils.dict_to_one( network.all_drop )
    feed_dict = {x: X_test, y_: y_test}
    feed_dict.update(dp_dict)
    print("   test loss: %f" % sess.run(cost, feed_dict=feed_dict))
    print("   test acc: %f" % np.mean(y_test == sess.run(y_op, feed_dict=feed_dict)))

    # Add ops to save and restore all the variables, including variables for training.
    # ref: https://www.tensorflow.org/versions/r0.8/how_tos/variables/index.html
    saver = tf.train.Saver()
    save_path = saver.save(sess, "model.ckpt")
    print("Model saved in file: %s" % save_path)


    # You can also save the parameters into .npz file.
    tl.files.save_npz(network.all_params , name='model.npz')
    # You can only save one parameter as follow.
    # tl.files.save_npz([network.all_params[0]] , name='model.npz')
    # Then, restore the parameters as follow.
    # load_params = tl.utils.load_npz(path='', name='model.npz')

    # In the end, close TensorFlow session.
    sess.close()

Example 45

View license
  def testBasic(self):
    """Test two batch entries."""
    # Input and ground truth from Alex Graves' implementation.
    #
    #### Batch entry 0 #####
    # targets: 0 1 2 1 0
    # outputs:
    # 0 0.633766 0.221185 0.0917319 0.0129757 0.0142857 0.0260553
    # 1 0.111121 0.588392 0.278779 0.0055756 0.00569609 0.010436
    # 2 0.0357786 0.633813 0.321418 0.00249248 0.00272882 0.0037688
    # 3 0.0663296 0.643849 0.280111 0.00283995 0.0035545 0.00331533
    # 4 0.458235 0.396634 0.123377 0.00648837 0.00903441 0.00623107
    # alpha:
    # 0 -3.64753 -0.456075 -inf -inf -inf -inf -inf -inf -inf -inf -inf
    # 1 -inf -inf -inf -0.986437 -inf -inf -inf -inf -inf -inf -inf
    # 2 -inf -inf -inf -inf -inf -2.12145 -inf -inf -inf -inf -inf
    # 3 -inf -inf -inf -inf -inf -inf -inf -2.56174 -inf -inf -inf
    # 4 -inf -inf -inf -inf -inf -inf -inf -inf -inf -3.34211 -inf
    # beta:
    # 0 -inf -2.88604 -inf -inf -inf -inf -inf -inf -inf -inf -inf
    # 1 -inf -inf -inf -2.35568 -inf -inf -inf -inf -inf -inf -inf
    # 2 -inf -inf -inf -inf -inf -1.22066 -inf -inf -inf -inf -inf
    # 3 -inf -inf -inf -inf -inf -inf -inf -0.780373 -inf -inf -inf
    # 4 -inf -inf -inf -inf -inf -inf -inf -inf -inf 0 0
    # prob: -3.34211
    # outputDerivs:
    # 0 -0.366234 0.221185 0.0917319 0.0129757 0.0142857 0.0260553
    # 1 0.111121 -0.411608 0.278779 0.0055756 0.00569609 0.010436
    # 2 0.0357786 0.633813 -0.678582 0.00249248 0.00272882 0.0037688
    # 3 0.0663296 -0.356151 0.280111 0.00283995 0.0035545 0.00331533
    # 4 -0.541765 0.396634 0.123377 0.00648837 0.00903441 0.00623107
    #
    #### Batch entry 1 #####
    #
    # targets: 0 1 1 0
    # outputs:
    # 0 0.30176 0.28562 0.0831517 0.0862751 0.0816851 0.161508
    # 1 0.24082 0.397533 0.0557226 0.0546814 0.0557528 0.19549
    # 2 0.230246 0.450868 0.0389607 0.038309 0.0391602 0.202456
    # 3 0.280884 0.429522 0.0326593 0.0339046 0.0326856 0.190345
    # 4 0.423286 0.315517 0.0338439 0.0393744 0.0339315 0.154046
    # alpha:
    # 0 -1.8232 -1.19812 -inf -inf -inf -inf -inf -inf -inf
    # 1 -inf -2.19315 -2.83037 -2.1206 -inf -inf -inf -inf -inf
    # 2 -inf -inf -inf -2.03268 -3.71783 -inf -inf -inf -inf
    # 3 -inf -inf -inf -inf -inf -4.56292 -inf -inf -inf
    # 4 -inf -inf -inf -inf -inf -inf -inf -5.42262 -inf
    # beta:
    # 0 -inf -4.2245 -inf -inf -inf -inf -inf -inf -inf
    # 1 -inf -inf -inf -3.30202 -inf -inf -inf -inf -inf
    # 2 -inf -inf -inf -inf -1.70479 -0.856738 -inf -inf -inf
    # 3 -inf -inf -inf -inf -inf -0.859706 -0.859706 -0.549337 -inf
    # 4 -inf -inf -inf -inf -inf -inf -inf 0 0
    # prob: -5.42262
    # outputDerivs:
    # 0 -0.69824 0.28562 0.0831517 0.0862751 0.0816851 0.161508
    # 1 0.24082 -0.602467 0.0557226 0.0546814 0.0557528 0.19549
    # 2 0.230246 0.450868 0.0389607 0.038309 0.0391602 -0.797544
    # 3 0.280884 -0.570478 0.0326593 0.0339046 0.0326856 0.190345
    # 4 -0.576714 0.315517 0.0338439 0.0393744 0.0339315 0.154046

    # max_time_steps == 7
    depth = 6

    # seq_len_0 == 5
    targets_0 = [0, 1, 2, 1, 0]
    loss_log_prob_0 = -3.34211
    # dimensions are time x depth
    input_prob_matrix_0 = np.asarray(
        [[0.633766, 0.221185, 0.0917319, 0.0129757, 0.0142857, 0.0260553],
         [0.111121, 0.588392, 0.278779, 0.0055756, 0.00569609, 0.010436],
         [0.0357786, 0.633813, 0.321418, 0.00249248, 0.00272882, 0.0037688],
         [0.0663296, 0.643849, 0.280111, 0.00283995, 0.0035545, 0.00331533],
         [0.458235, 0.396634, 0.123377, 0.00648837, 0.00903441, 0.00623107]],
        dtype=np.float32)
    input_log_prob_matrix_0 = np.log(input_prob_matrix_0)
    gradient_log_prob_0 = np.asarray(
        [[-0.366234, 0.221185, 0.0917319, 0.0129757, 0.0142857, 0.0260553],
         [0.111121, -0.411608, 0.278779, 0.0055756, 0.00569609, 0.010436],
         [0.0357786, 0.633813, -0.678582, 0.00249248, 0.00272882, 0.0037688],
         [0.0663296, -0.356151, 0.280111, 0.00283995, 0.0035545, 0.00331533],
         [-0.541765, 0.396634, 0.123377, 0.00648837, 0.00903441, 0.00623107]],
        dtype=np.float32)

    # seq_len_1 == 5
    targets_1 = [0, 1, 1, 0]
    loss_log_prob_1 = -5.42262
    # dimensions are time x depth

    input_prob_matrix_1 = np.asarray(
        [[0.30176, 0.28562, 0.0831517, 0.0862751, 0.0816851, 0.161508],
         [0.24082, 0.397533, 0.0557226, 0.0546814, 0.0557528, 0.19549],
         [0.230246, 0.450868, 0.0389607, 0.038309, 0.0391602, 0.202456],
         [0.280884, 0.429522, 0.0326593, 0.0339046, 0.0326856, 0.190345],
         [0.423286, 0.315517, 0.0338439, 0.0393744, 0.0339315, 0.154046]],
        dtype=np.float32)
    input_log_prob_matrix_1 = np.log(input_prob_matrix_1)
    gradient_log_prob_1 = np.asarray(
        [[-0.69824, 0.28562, 0.0831517, 0.0862751, 0.0816851, 0.161508],
         [0.24082, -0.602467, 0.0557226, 0.0546814, 0.0557528, 0.19549],
         [0.230246, 0.450868, 0.0389607, 0.038309, 0.0391602, -0.797544],
         [0.280884, -0.570478, 0.0326593, 0.0339046, 0.0326856, 0.190345],
         [-0.576714, 0.315517, 0.0338439, 0.0393744, 0.0339315, 0.154046]],
        dtype=np.float32)

    # len max_time_steps array of 2 x depth matrices
    inputs = [np.vstack([input_log_prob_matrix_0[t, :],
                         input_log_prob_matrix_1[t, :]])
              for t in range(5)] + 2 * [np.nan*np.ones((2, depth), np.float32)]

    # convert inputs into [max_time x batch_size x depth tensor] Tensor
    inputs = np.asarray(inputs, dtype=np.float32)

    # len batch_size array of label vectors
    labels = SimpleSparseTensorFrom([targets_0, targets_1])

    # batch_size length vector of sequence_lengths
    seq_lens = np.array([5, 5], dtype=np.int32)

    # output: batch_size length vector of negative log probabilities
    loss_truth = np.array([-loss_log_prob_0, -loss_log_prob_1], np.float32)

    # output: len max_time_steps array of 2 x depth matrices
    grad_truth = [np.vstack([gradient_log_prob_0[t, :],
                             gradient_log_prob_1[t, :]])
                  for t in range(5)] + 2 * [np.zeros((2, depth), np.float32)]

    # convert grad_truth into [max_time x batch_size x depth] Tensor
    grad_truth = np.asarray(grad_truth, dtype=np.float32)

    self._testCTCLoss(inputs, seq_lens, labels, loss_truth, grad_truth)

Example 46

View license
def main_test_stacked_denoise_AE(model='relu'):
    # Load MNIST dataset
    X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_mnist_dataset(shape=(-1,784))

    X_train = np.asarray(X_train, dtype=np.float32)
    y_train = np.asarray(y_train, dtype=np.int64)
    X_val = np.asarray(X_val, dtype=np.float32)
    y_val = np.asarray(y_val, dtype=np.int64)
    X_test = np.asarray(X_test, dtype=np.float32)
    y_test = np.asarray(y_test, dtype=np.int64)

    print('X_train.shape', X_train.shape)
    print('y_train.shape', y_train.shape)
    print('X_val.shape', X_val.shape)
    print('y_val.shape', y_val.shape)
    print('X_test.shape', X_test.shape)
    print('y_test.shape', y_test.shape)
    print('X %s   y %s' % (X_test.dtype, y_test.dtype))

    sess = tf.InteractiveSession()

    x = tf.placeholder(tf.float32, shape=[None, 784], name='x')
    y_ = tf.placeholder(tf.int64, shape=[None, ], name='y_')

    if model == 'relu':
        act = tf.nn.relu
        act_recon = tf.nn.softplus
    elif model == 'sigmoid':
        act = tf.nn.sigmoid
        act_recon = act

    # Define network
    print("\nBuild Network")
    network = tl.layers.InputLayer(x, name='input_layer')
    # denoise layer for AE
    network = tl.layers.DropoutLayer(network, keep=0.5, name='denoising1')
    # 1st layer
    network = tl.layers.DropoutLayer(network, keep=0.8, name='drop1')
    network = tl.layers.DenseLayer(network, n_units=800, act = act, name=model+'1')
    x_recon1 = network.outputs
    recon_layer1 = tl.layers.ReconLayer(network, x_recon=x, n_units=784, act = act_recon, name='recon_layer1')
    # 2nd layer
    network = tl.layers.DropoutLayer(network, keep=0.5, name='drop2')
    network = tl.layers.DenseLayer(network, n_units=800, act = act, name=model+'2')
    recon_layer2 = tl.layers.ReconLayer(network, x_recon=x_recon1, n_units=800, act = act_recon, name='recon_layer2')
    # 3rd layer
    network = tl.layers.DropoutLayer(network, keep=0.5, name='drop3')
    network = tl.layers.DenseLayer(network, n_units=10, act = tl.activation.identity, name='output_layer')

    # Define fine-tune process
    y = network.outputs
    y_op = tf.argmax(tf.nn.softmax(y), 1)
    ce = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(y, y_))
    cost = ce

    n_epoch = 200
    batch_size = 128
    learning_rate = 0.0001
    print_freq = 10

    train_params = network.all_params

        # train_op = tf.train.GradientDescentOptimizer(0.5).minimize(cost)
    train_op = tf.train.AdamOptimizer(learning_rate , beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False).minimize(cost, var_list=train_params)

    # Initialize all variables including weights, biases and the variables in train_op
    sess.run(tf.initialize_all_variables())

    # Pre-train
    print("\nAll Network Params before pre-train")
    network.print_params()
    print("\nPre-train Layer 1")
    recon_layer1.pretrain(sess, x=x, X_train=X_train, X_val=X_val, denoise_name='denoising1', n_epoch=100, batch_size=128, print_freq=10, save=True, save_name='w1pre_')
    print("\nPre-train Layer 2")
    recon_layer2.pretrain(sess, x=x, X_train=X_train, X_val=X_val, denoise_name='denoising1', n_epoch=100, batch_size=128, print_freq=10, save=False)
    print("\nAll Network Params after pre-train")
    network.print_params()

    # Fine-tune
    print("\nFine-tune Network")
    correct_prediction = tf.equal(tf.argmax(y, 1), y_)
    acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    print('   learning_rate: %f' % learning_rate)
    print('   batch_size: %d' % batch_size)

    for epoch in range(n_epoch):
        start_time = time.time()
        for X_train_a, y_train_a in tl.iterate.minibatches(X_train, y_train, batch_size, shuffle=True):
            feed_dict = {x: X_train_a, y_: y_train_a}
            feed_dict.update( network.all_drop )        # enable all dropout/dropconnect/denoising layers
            feed_dict[set_keep['denoising1']] = 1    # disable denoising layer
            sess.run(train_op, feed_dict=feed_dict)

        if epoch + 1 == 1 or (epoch + 1) % print_freq == 0:
            print("Epoch %d of %d took %fs" % (epoch + 1, n_epoch, time.time() - start_time))
            train_loss, train_acc, n_batch = 0, 0, 0
            for X_train_a, y_train_a in tl.iterate.minibatches(X_train, y_train, batch_size, shuffle=True):
                dp_dict = tl.utils.dict_to_one( network.all_drop )    # disable all dropout/dropconnect/denoising layers
                feed_dict = {x: X_train_a, y_: y_train_a}
                feed_dict.update(dp_dict)
                err, ac = sess.run([cost, acc], feed_dict=feed_dict)
                train_loss += err
                train_acc += ac
                n_batch += 1
            print("   train loss: %f" % (train_loss/ n_batch))
            print("   train acc: %f" % (train_acc/ n_batch))
            val_loss, val_acc, n_batch = 0, 0, 0
            for X_val_a, y_val_a in tl.iterate.minibatches(X_val, y_val, batch_size, shuffle=True):
                dp_dict = tl.utils.dict_to_one( network.all_drop )    # disable all dropout/dropconnect/denoising layers
                feed_dict = {x: X_val_a, y_: y_val_a}
                feed_dict.update(dp_dict)
                err, ac = sess.run([cost, acc], feed_dict=feed_dict)
                val_loss += err
                val_acc += ac
                n_batch += 1
            print("   val loss: %f" % (val_loss/ n_batch))
            print("   val acc: %f" % (val_acc/ n_batch))
            try:
                tl.visualize.W(network.all_params[0].eval(), second=10, saveable=True, shape=[28, 28], name='w1_'+str(epoch+1), fig_idx=2012)
            except:
                raise Exception("# You should change visualize_W(), if you want to save the feature images for different dataset")

    print('Evaluation')
    test_loss, test_acc, n_batch = 0, 0, 0
    for X_test_a, y_test_a in tl.iterate.minibatches(X_test, y_test, batch_size, shuffle=True):
        dp_dict = tl.utils.dict_to_one( network.all_drop )    # disable all dropout layers
        feed_dict = {x: X_test_a, y_: y_test_a}
        feed_dict.update(dp_dict)
        err, ac = sess.run([cost, acc], feed_dict=feed_dict)
        test_loss += err
        test_acc += ac
        n_batch += 1
    print("   test loss: %f" % (test_loss/n_batch))
    print("   test acc: %f" % (test_acc/n_batch))
        # print("   test acc: %f" % np.mean(y_test == sess.run(y_op, feed_dict=feed_dict)))

    # Add ops to save and restore all the variables.
    # ref: https://www.tensorflow.org/versions/r0.8/how_tos/variables/index.html
    saver = tf.train.Saver()
    # you may want to save the model
    save_path = saver.save(sess, "model.ckpt")
    print("Model saved in file: %s" % save_path)
    sess.close()

Example 47

Project: Emotion-Recognition-RNN
Source File: trainer.py
View license
    def __init__(self, model=None, inputs=None, outputs=None, masks=None,
                 batchsize=100, learningrate=.01,
                 momentum=0.9, loadsize=None,
                 rng=None, verbose=True,
                 gradient_clip_threshold=1000,
                 numepochs_per_load=1,
                 rmsprop=False, cost=None, params=None, inputvar=None,
                 grads=None, monitor_update_weight_norm_ratio=False,
                 auto_reset_on_naninf=True,
                 monitor_by_epoch_not_load=False,
                 targetvar=None, maskvar=None, preprocess_fn=None):

        self.verbose = verbose
        self.monitor_by_epoch_not_load = monitor_by_epoch_not_load
        if not hasattr(preprocess_fn, '__call__'):
            preprocess_fn = lambda x: x
        self.preprocess_fn = preprocess_fn


#{{{ Initialization of Properties
        assert model is not None or (
            cost is not None and params is not None and
            inputvar is not None and grads is not None
        ), (
            "either a model instance or cost, params and inputvar "
            "have to be passed to the SGD_Trainer constructor")

        self.auto_reset_on_naninf = auto_reset_on_naninf
        self.monitor_update_weight_norm_ratio = monitor_update_weight_norm_ratio
        print 'monitor_update_weight_norm_ratio: {0}'.format(monitor_update_weight_norm_ratio, )

        self.is_supervised = False
        self.has_masks = False

        self._inputs_type = 'numpy'
        self._inputs = inputs
        if outputs is not None:
            self._outputs = outputs
            self.is_supervised = True
        if masks is not None:
            self.has_masks = True
            self._masks = masks

        if model is not None:
            self._model = model
            self._params = model.params
            self._cost = model._cost
            self._inputvar = model.inputs
            self._grads = model._grads
            if self.is_supervised:
                try:
                    self._targetvar = model.targets
                except AttributeError:
                    print 'Training in supervised mode, but model does not have target variable'
                    raise
            if self.has_masks:
                try:
                    self._maskvar = model.masks
                except AttributeError:
                    print 'Training in masked cost mode, but model does not have mask variable'
                    raise
        else:
            self._params = params
            self._cost = cost
            self._inputvar = inputvar
            self._grads = grads
            if self.is_supervised:
                try:
                    self._targetvar = targetvar
                except AttributeError:
                    print 'Training in supervised mode, but no target variable specified'
                    raise
            if self.has_masks:
                try:
                    self._maskvar = maskvar
                except AttributeError:
                    print 'Training in masked cost mode, but no mask variable specified'
                    raise

        # compute total number of params
        self._nparams = 0
        for p in self._params:
            try:
                self._nparams += p.get_value().size
            except AttributeError:
                # handles scalar params
                self._nparams += 1
        print 'number of params: {0}'.format(self._nparams)

        if monitor_update_weight_norm_ratio:
            self._update_weight_norm_ratios_log = dict(
                [(p, []) for p in self._params])

        self._learningrate = theano.shared(np.float32(learningrate),
                                           name='learningrate')
        self.numepochs_per_load = numepochs_per_load

        self._momentum = theano.shared(np.float32(momentum),
                                       name='momentum')
        self._total_stepcount = 0

        self._gradient_clip_threshold = theano.shared(
            np.float32(gradient_clip_threshold),
            name='gradient_clip_threshold')
        self._avg_gradnorm = theano.shared(np.float32(0.), name='avg_gradnorm')

        self._model = model

        self._numparams = reduce(lambda x, y: x + y,
                                 [p.get_value().size for p in self._params])

        self.numcases = self._inputs.shape[0]

        self.batchsize = batchsize
        self.loadsize = loadsize
        if self.batchsize > self.loadsize:
            warnings.warn('batchsize > loadsize -> batchsize := loadsize')
            self.batchsize = self.loadsize

        if self.loadsize % self.batchsize:
            self.loadsize = int(
                self.loadsize // self.batchsize + 1) * self.batchsize

        if rng is None:
            self._rng = np.random.RandomState(1)
        else:
            self._rng = rng

        # prepare loads
        self.prepare_loads()

        # use first load to allocate shared variable
        self._inputs_theano = theano.shared(
            self._inputs[self.data_indices[0]],
            name='inputs')
        if self.is_supervised:
            self._outputs_theano = theano.shared(
                self._outputs[self.data_indices[0]],
                name='outputs')
        if self.has_masks:
            self._masks_theano = theano.shared(
                self._masks[self.data_indices[0]],
                name='masks')

        self._momentum_batchcounter = 0

        self._epochcount = 0
        self._index = T.lscalar()
        self._incs = \
            dict([(p, theano.shared(
                value=np.zeros(p.get_value().shape,
                               dtype=theano.config.floatX),
                name='inc_' + p.name))
                for p in self._params])
        self._inc_updates = collections.OrderedDict()
        self.rmsprop = rmsprop
        if self.rmsprop:
            self.averaging_coeff = 0.95
            self.stabilizer = 1e-2
            self._avg_grad_sqrs = \
                dict([(p, theano.shared(value=np.zeros(
                    p.get_value().shape,
                    dtype=theano.config.floatX), name='avg_grad_sqr_' + p.name))
                    for p in self._params])
        self._avg_grad_sqrs_updates = collections.OrderedDict()
        self._updates_nomomentum = collections.OrderedDict()
        self._updates = collections.OrderedDict()
        self._n = T.lscalar('n')
        self._n.tag.test_value = 0.
        self._noop = 0.0 * self._n
        self._batch_idx = theano.shared(
            value=np.array(0, dtype=np.int64), name='batch_idx')

        self.costs = []
        self._compile_functions()

Example 48

View license
def main_test_cnn_layer():
    '''
        Reimplementation of the tensorflow official MNIST CNN tutorials:
        # https://www.tensorflow.org/versions/r0.8/tutorials/mnist/pros/index.html
        # https://github.com/tensorflow/tensorflow/blob/master/tensorflow/models/image/mnist/convolutional.py
        More TensorFlow official CNN tutorials can be found here:
        # https://www.tensorflow.org/versions/master/tutorials/deep_cnn/index.html
    '''
    X_train, y_train, X_val, y_val, X_test, y_test = tl.files.load_mnist_dataset(shape=(-1, 28, 28, 1))

    X_train = np.asarray(X_train, dtype=np.float32)
    y_train = np.asarray(y_train, dtype=np.int64)
    X_val = np.asarray(X_val, dtype=np.float32)
    y_val = np.asarray(y_val, dtype=np.int64)
    X_test = np.asarray(X_test, dtype=np.float32)
    y_test = np.asarray(y_test, dtype=np.int64)

    print('X_train.shape', X_train.shape)
    print('y_train.shape', y_train.shape)
    print('X_val.shape', X_val.shape)
    print('y_val.shape', y_val.shape)
    print('X_test.shape', X_test.shape)
    print('y_test.shape', y_test.shape)
    print('X %s   y %s' % (X_test.dtype, y_test.dtype))

    sess = tf.InteractiveSession()

    # Define the batchsize at the begin, you can give the batchsize in x and y_
    # rather than 'None', this can allow TensorFlow to apply some optimizations
    # – especially for convolutional layers.
    batch_size = 128

    x = tf.placeholder(tf.float32, shape=[batch_size, 28, 28, 1])   # [batch_size, height, width, channels]
    y_ = tf.placeholder(tf.int64, shape=[batch_size,])

    network = tl.layers.InputLayer(x, name='input_layer')
    network = tl.layers.Conv2dLayer(network,
                        act = tf.nn.relu,
                        shape = [5, 5, 1, 32],  # 32 features for each 5x5 patch
                        strides=[1, 1, 1, 1],
                        padding='SAME',
                        name ='cnn_layer1')     # output: (?, 28, 28, 32)
    # Alternatively, you can choose a specific initializer for the weights as follow:
    # network = tl.layers.Conv2dLayer(network,
    #                     act = tf.nn.relu,
    #                     shape = [5, 5, 1, 32],  # 32 features for each 5x5 patch
    #                     strides=[1, 1, 1, 1],
    #                     padding='SAME',
    #                     W_init = tf.truncated_normal,
    #                     W_init_args = {'mean' : 1, 'stddev':3},
    #                     b_init = tf.zeros,
    #                     b_init_args = {'name' : 'bias'},
    #                     name ='cnn_layer1')     # output: (?, 28, 28, 32)
    network = tl.layers.PoolLayer(network,
                        ksize=[1, 2, 2, 1],
                        strides=[1, 2, 2, 1],
                        padding='SAME',
                        pool = tf.nn.max_pool,
                        name ='pool_layer1',)   # output: (?, 14, 14, 32)
    network = tl.layers.Conv2dLayer(network,
                        act = tf.nn.relu,
                        shape = [5, 5, 32, 64], # 64 features for each 5x5 patch
                        strides=[1, 1, 1, 1],
                        padding='SAME',
                        name ='cnn_layer2')     # output: (?, 14, 14, 64)
    network = tl.layers.PoolLayer(network,
                        ksize=[1, 2, 2, 1],
                        strides=[1, 2, 2, 1],
                        padding='SAME',
                        pool = tf.nn.max_pool,
                        name ='pool_layer2',)   # output: (?, 7, 7, 64)
    network = tl.layers.FlattenLayer(network, name='flatten_layer')                                # output: (?, 3136)
    network = tl.layers.DropoutLayer(network, keep=0.5, name='drop1')                              # output: (?, 3136)
    network = tl.layers.DenseLayer(network, n_units=256, act = tf.nn.relu, name='relu1')           # output: (?, 256)
    network = tl.layers.DropoutLayer(network, keep=0.5, name='drop2')                              # output: (?, 256)
    network = tl.layers.DenseLayer(network, n_units=10, act = tl.activation.identity, name='output_layer')    # output: (?, 10)

    y = network.outputs

    ce = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(y, y_))
    cost = ce

    correct_prediction = tf.equal(tf.argmax(y, 1), y_)
    acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # train
    n_epoch = 100
    learning_rate = 0.0001
    print_freq = 10

    train_params = network.all_params
    train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999, epsilon=1e-08, use_locking=False).minimize(cost, var_list=train_params)

    sess.run(tf.initialize_all_variables())
    network.print_params()
    network.print_layers()

    print('   learning_rate: %f' % learning_rate)
    print('   batch_size: %d' % batch_size)

    for epoch in range(n_epoch):
        start_time = time.time()
        for X_train_a, y_train_a in tl.iterate.minibatches(X_train, y_train, batch_size, shuffle=True):
            feed_dict = {x: X_train_a, y_: y_train_a}
            feed_dict.update( network.all_drop )        # enable all dropout/dropconnect/denoising layers
            sess.run(train_op, feed_dict=feed_dict)

        if epoch + 1 == 1 or (epoch + 1) % print_freq == 0:
            print("Epoch %d of %d took %fs" % (epoch + 1, n_epoch, time.time() - start_time))
            train_loss, train_acc, n_batch = 0, 0, 0
            for X_train_a, y_train_a in tl.iterate.minibatches(X_train, y_train, batch_size, shuffle=True):
                dp_dict = tl.utils.dict_to_one( network.all_drop )    # disable all dropout/dropconnect/denoising layers
                feed_dict = {x: X_train_a, y_: y_train_a}
                feed_dict.update(dp_dict)
                err, ac = sess.run([cost, acc], feed_dict=feed_dict)
                train_loss += err; train_acc += ac; n_batch += 1
            print("   train loss: %f" % (train_loss/ n_batch))
            print("   train acc: %f" % (train_acc/ n_batch))
            val_loss, val_acc, n_batch = 0, 0, 0
            for X_val_a, y_val_a in tl.iterate.minibatches(X_val, y_val, batch_size, shuffle=True):
                dp_dict = tl.utils.dict_to_one( network.all_drop )    # disable all dropout/dropconnect/denoising layers
                feed_dict = {x: X_val_a, y_: y_val_a}
                feed_dict.update(dp_dict)
                err, ac = sess.run([cost, acc], feed_dict=feed_dict)
                val_loss += err; val_acc += ac; n_batch += 1
            print("   val loss: %f" % (val_loss/ n_batch))
            print("   val acc: %f" % (val_acc/ n_batch))
            try:
                tl.visualize.CNN2d(network.all_params[0].eval(), second=10, saveable=True, name='cnn1_'+str(epoch+1), fig_idx=2012)
            except:
                raise Exception("# You should change visualize.CNN(), if you want to save the feature images for different dataset")

    print('Evaluation')
    test_loss, test_acc, n_batch = 0, 0, 0
    for X_test_a, y_test_a in tl.iterate.minibatches(X_test, y_test, batch_size, shuffle=True):
        dp_dict = tl.utils.dict_to_one( network.all_drop )    # disable all dropout/dropconnect/denoising layers
        feed_dict = {x: X_test_a, y_: y_test_a}
        feed_dict.update(dp_dict)
        err, ac = sess.run([cost, acc], feed_dict=feed_dict)
        test_loss += err; test_acc += ac; n_batch += 1
    print("   test loss: %f" % (test_loss/n_batch))
    print("   test acc: %f" % (test_acc/n_batch))

Example 49

View license
def main_test_cnn_naive():
    """Without any distorting, whitening and cropping for training data.
    This method work well for MNIST, but not CIFAR-10.
    """
    model_file_name = "model_cifar10_naive.ckpt"
    resume = False # load model, resume from previous checkpoint?

    X_train, y_train, X_test, y_test = tl.files.load_cifar10_dataset(
                                        shape=(-1, 32, 32, 3), plotable=False)

    X_train = np.asarray(X_train, dtype=np.float32)
    y_train = np.asarray(y_train, dtype=np.int32)
    X_test = np.asarray(X_test, dtype=np.float32)
    y_test = np.asarray(y_test, dtype=np.int32)

    print('X_train.shape', X_train.shape)   # (50000, 32, 32, 3)
    print('y_train.shape', y_train.shape)   # (50000,)
    print('X_test.shape', X_test.shape)     # (10000, 32, 32, 3)
    print('y_test.shape', y_test.shape)     # (10000,)
    print('X %s   y %s' % (X_test.dtype, y_test.dtype))

    sess = tf.InteractiveSession()

    batch_size = 128

    x = tf.placeholder(tf.float32, shape=[batch_size, 32, 32, 3])
                                # [batch_size, height, width, channels]
    y_ = tf.placeholder(tf.int32, shape=[batch_size,])

    network = tl.layers.InputLayer(x, name='input_layer')
    network = tl.layers.Conv2dLayer(network,
                        act = tf.nn.relu,
                        shape = [5, 5, 3, 64],  # 64 features for each 5x5x3 patch
                        strides=[1, 1, 1, 1],
                        padding='SAME',
                        name ='cnn_layer1')     # output: (?, 32, 32, 64)
    network = tl.layers.PoolLayer(network,
                        ksize=[1, 3, 3, 1],
                        strides=[1, 2, 2, 1],
                        padding='SAME',
                        pool = tf.nn.max_pool,
                        name ='pool_layer1')   # output: (?, 16, 16, 64)
    # local response normalization, you can also try batch normalization.
    # References: ImageNet Classification with Deep Convolutional Neural Networks
    #   it increases the accuracy but consume more time.
    #     https://www.tensorflow.org/versions/master/api_docs/python/nn.html#local_response_normalization
    network.outputs = tf.nn.lrn(network.outputs, 4, bias=1.0, alpha=0.001 / 9.0,
                                                        beta=0.75, name='norm1')
    network = tl.layers.Conv2dLayer(network,
                        act = tf.nn.relu,
                        shape = [5, 5, 64, 64], # 64 features for each 5x5 patch
                        strides=[1, 1, 1, 1],
                        padding='SAME',
                        name ='cnn_layer2')     # output: (?, 16, 16, 64)
    # Another local response normalization.
    network.outputs = tf.nn.lrn(network.outputs, 4, bias=1.0, alpha=0.001 / 9.0,
                                                        beta=0.75, name='norm2')
    network = tl.layers.PoolLayer(network,
                        ksize=[1, 3, 3, 1],
                        strides=[1, 2, 2, 1],
                        padding='SAME',
                        pool = tf.nn.max_pool,
                        name ='pool_layer2')   # output: (?, 8, 8, 64)

    network = tl.layers.FlattenLayer(network, name='flatten_layer')
                                                            # output: (?, 4096)
    network = tl.layers.DenseLayer(network, n_units=384,
                            act = tf.nn.relu, name='relu1') # output: (?, 384)
    network = tl.layers.DenseLayer(network, n_units=192,
                            act = tf.nn.relu, name='relu2') # output: (?, 192)
    network = tl.layers.DenseLayer(network, n_units=10,
                            act = tf.identity,
                            name='output_layer')            # output: (?, 10)

    y = network.outputs

    ce = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(y, y_))
    cost = ce

    correct_prediction = tf.equal(tf.cast(tf.argmax(y, 1), tf.int32), y_)
    acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    # train
    n_epoch = 10000
    learning_rate = 0.0001
    print_freq = 1

    train_params = network.all_params
    train_op = tf.train.AdamOptimizer(learning_rate, beta1=0.9, beta2=0.999,
        epsilon=1e-08, use_locking=False).minimize(cost, var_list=train_params)

    sess.run(tf.initialize_all_variables())
    if resume:
        print("Load existing model " + "!"*10)
        saver = tf.train.Saver()
        saver.restore(sess, model_file_name)

    network.print_params()
    network.print_layers()

    print('   learning_rate: %f' % learning_rate)
    print('   batch_size: %d' % batch_size)

    for epoch in range(n_epoch):
        start_time = time.time()
        for X_train_a, y_train_a in tl.iterate.minibatches(
                                X_train, y_train, batch_size, shuffle=True):
            feed_dict = {x: X_train_a, y_: y_train_a}
            feed_dict.update( network.all_drop )        # enable all dropout/dropconnect/denoising layers
            sess.run(train_op, feed_dict=feed_dict)

        if epoch + 1 == 1 or (epoch + 1) % print_freq == 0:
            print("Epoch %d of %d took %fs" % (epoch + 1, n_epoch, time.time() - start_time))
            train_loss, train_acc, n_batch = 0, 0, 0
            for X_train_a, y_train_a in tl.iterate.minibatches(
                                    X_train, y_train, batch_size, shuffle=True):
                dp_dict = tl.utils.dict_to_one( network.all_drop )    # disable all dropout/dropconnect/denoising layers
                feed_dict = {x: X_train_a, y_: y_train_a}
                feed_dict.update(dp_dict)
                err, ac = sess.run([cost, acc], feed_dict=feed_dict)
                assert not np.isnan(err), 'Model diverged with cost = NaN'
                train_loss += err; train_acc += ac; n_batch += 1
            print("   train loss: %f" % (train_loss/ n_batch))
            print("   train acc: %f" % (train_acc/ n_batch))
            test_loss, test_acc, n_batch = 0, 0, 0
            for X_test_a, y_test_a in tl.iterate.minibatches(
                                    X_test, y_test, batch_size, shuffle=True):
                dp_dict = tl.utils.dict_to_one( network.all_drop )    # disable all dropout/dropconnect/denoising layers
                feed_dict = {x: X_test_a, y_: y_test_a}
                feed_dict.update(dp_dict)
                err, ac = sess.run([cost, acc], feed_dict=feed_dict)
                test_loss += err; test_acc += ac; n_batch += 1
            print("   test loss: %f" % (test_loss/ n_batch))
            print("   test acc: %f" % (test_acc/ n_batch))
            try:
                pass
                # tl.visualize.CNN2d(network.all_params[0].eval(), second=10, saveable=True, name='cnn1_'+str(epoch+1), fig_idx=2012)
            except:
                raise Exception("# You should change visualize.CNN(), \
                if you want to save the feature images for different dataset")

        if (epoch + 1) % 1 == 0:
            print("Save model " + "!"*10);
            saver = tf.train.Saver()
            save_path = saver.save(sess, model_file_name)

Example 50

Project: lowrank-gru
Source File: RNN_adding.py
View license
def main(n_iter, n_batch, n_hidden, time_steps, learning_rate, savefile, scale_penalty, use_scale,
         model, n_hidden_lstm, loss_function, n_gru_lr_proj, initial_b_u):

    #import pdb; pdb.set_trace()
 
    # --- Set optimization params --------
    gradient_clipping = np.float32(50000)

    # --- Set data params ----------------
    n_input = 2
    n_output = 1
  

    # --- Manage data --------------------
    n_train = 1e5
    n_test = 1e4
    num_batches = n_train / n_batch
    
    train_x = np.asarray(np.zeros((time_steps, n_train, 2)),
                         dtype=theano.config.floatX)
    

    train_x[:,:,0] = np.asarray(np.random.uniform(low=0.,
                                                  high=1.,
                                                  size=(time_steps, n_train)),
                                dtype=theano.config.floatX)
    
#    inds = np.asarray([np.random.choice(time_steps, 2, replace=False) for i in xrange(train_x.shape[1])])    
    inds = np.asarray(np.random.randint(time_steps/2, size=(train_x.shape[1],2)))
    inds[:, 1] += time_steps/2  
    
    for i in range(train_x.shape[1]):
        train_x[inds[i, 0], i, 1] = 1.0
        train_x[inds[i, 1], i, 1] = 1.0
 
    train_y = (train_x[:,:,0] * train_x[:,:,1]).sum(axis=0)
    train_y = np.reshape(train_y, (n_train, 1))

    test_x = np.asarray(np.zeros((time_steps, n_test, 2)),
                        dtype=theano.config.floatX)
    

    test_x[:,:,0] = np.asarray(np.random.uniform(low=0.,
                                                 high=1.,
                                                 size=(time_steps, n_test)),
                                dtype=theano.config.floatX)
    
    inds = np.asarray([np.random.choice(time_steps, 2, replace=False) for i in xrange(test_x.shape[1])])    
    for i in range(test_x.shape[1]):
        test_x[inds[i, 0], i, 1] = 1.0
        test_x[inds[i, 1], i, 1] = 1.0
 
   
    test_y = (test_x[:,:,0] * test_x[:,:,1]).sum(axis=0)
    test_y = np.reshape(test_y, (n_test, 1)) 


   #######################################################################

    gradient_clipping = np.float32(1)

    if (model == 'LSTM'):   
        #inputs, parameters, costs = LSTM(n_input, n_hidden_lstm, n_output, loss_function=loss_function)
        inputs, parameters, costs = LSTM(n_input, n_hidden_lstm, n_output, loss_function=loss_function, initial_b_f=initial_b_u)
        gradients = T.grad(costs[0], parameters)
        gradients = [T.clip(g, -gradient_clipping, gradient_clipping) for g in gradients]
    
    #by AnvaMiba
    elif (model == 'GRU'):   
        inputs, parameters, costs = GRU(n_input, n_hidden_lstm, n_output, loss_function=loss_function, initial_b_u=initial_b_u)
        gradients = T.grad(costs[0], parameters)
        gradients = [T.clip(g, -gradient_clipping, gradient_clipping) for g in gradients]
    #by AnvaMiba
    elif (model == 'GRU_LR'):   
        inputs, parameters, costs = GRU_LR(n_input, n_hidden_lstm, n_output, n_gru_lr_proj, loss_function=loss_function, initial_b_u=initial_b_u)
        gradients = T.grad(costs[0], parameters)
        gradients = [T.clip(g, -gradient_clipping, gradient_clipping) for g in gradients]

    elif (model == 'complex_RNN'):
        inputs, parameters, costs = complex_RNN(n_input, n_hidden, n_output, scale_penalty, loss_function=loss_function)
        if use_scale is False:
            parameters.pop()
        gradients = T.grad(costs[0], parameters)

    elif (model == 'complex_RNN_LSTM'):
        inputs, parameters, costs = complex_RNN_LSTM(n_input, n_hidden, n_hidden_lstm, n_output, scale_penalty, loss_function=loss_function)

    elif (model == 'IRNN'):
        inputs, parameters, costs = IRNN(n_input, n_hidden, n_output, loss_function=loss_function)
        gradients = T.grad(costs[0], parameters)
        gradients = [T.clip(g, -gradient_clipping, gradient_clipping) for g in gradients]

    elif (model == 'RNN'):
        inputs, parameters, costs = tanhRNN(n_input, n_hidden, n_output, loss_function=loss_function)
        gradients = T.grad(costs[0], parameters)
        gradients = [T.clip(g, -gradient_clipping, gradient_clipping) for g in gradients]

    else:
        print >> sys.stderr, "Unsuported model:", model
        return
 

   




    s_train_x = theano.shared(train_x)
    s_train_y = theano.shared(train_y)

    s_test_x = theano.shared(test_x)
    s_test_y = theano.shared(test_y)


    # --- Compile theano functions --------------------------------------------------

    index = T.iscalar('i')

    updates, rmsprop = rms_prop(learning_rate, parameters, gradients)

    givens = {inputs[0] : s_train_x[:, n_batch * index : n_batch * (index + 1), :],
              inputs[1] : s_train_y[n_batch * index : n_batch * (index + 1), :]}

    givens_test = {inputs[0] : s_test_x,
                   inputs[1] : s_test_y}
    
   
    
    train = theano.function([index], costs[0], givens=givens, updates=updates)
    test = theano.function([], costs[1], givens=givens_test)

    # --- Training Loop ---------------------------------------------------------------

    # f1 = file('/data/lisatmp3/shahamar/adding/complexRNN_400.pkl', 'rb')
    # data1 = cPickle.load(f1)
    # f1.close()
    # train_loss = data1['train_loss']
    # test_loss = data1['test_loss']
    # best_params = data1['best_params']
    # best_test_loss = data1['best_test_loss']

    # for i in xrange(len(parameters)):
    #     parameters[i].set_value(data1['parameters'][i])

    # for i in xrange(len(parameters)):
    #     rmsprop[i].set_value(data1['rmsprop'][i])
    
#    import pdb; pdb.set_trace()

    train_loss = []
    test_loss = []
    best_params = [p.get_value() for p in parameters]
    best_test_loss = 1e6
    for i in xrange(n_iter):
#        start_time = timeit.default_timer()


        if (n_iter % int(num_batches) == 0):
            #import pdb; pdb.set_trace()
            inds = np.random.permutation(int(n_train))
            data_x = s_train_x.get_value()
            s_train_x.set_value(data_x[:,inds,:])
            data_y = s_train_y.get_value()
            s_train_y.set_value(data_y[inds,:])


        mse = train(i % int(num_batches))
        train_loss.append(mse)
        print >> sys.stderr, "Iteration:", i
        print >> sys.stderr, "mse:", mse
        print >> sys.stderr, ''

        #if (i % 50==0):
        if (i % 500==0):
            mse = test()
            print >> sys.stderr, ''
            print >> sys.stderr, "TEST"
            print >> sys.stderr, "mse:", mse
            print >> sys.stderr, '' 
            test_loss.append(mse)

            if mse < best_test_loss:
                best_params = [p.get_value() for p in parameters]
                best_test_loss = mse

            
            save_vals = {'parameters': [p.get_value() for p in parameters],
                         'rmsprop': [r.get_value() for r in rmsprop],
                         'train_loss': train_loss,
                         'test_loss': test_loss,
                         'best_params': best_params,
                         'best_test_loss': best_test_loss,
                         'model': model,
                         'time_steps': time_steps}

            cPickle.dump(save_vals,
                         file(savefile, 'wb'),
                         cPickle.HIGHEST_PROTOCOL)