torch.from_numpy.float - python examples

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

145 Examples 7

3 View Complete Implementation : incremental_model_text_kernel_chaplot_resnet.py
Copyright GNU General Public License v3.0
Author : lil-lab
    def get_probs(self, agent_observed_state, model_state, mode=None, volatile=False):

        astert isinstance(agent_observed_state, AgentObservedState)
        agent_observed_state_list = [agent_observed_state]

        image_seqs = [[aos.get_last_image()]
                      for aos in agent_observed_state_list]
        image_batch = cuda_var(torch.from_numpy(np.array(image_seqs)).float(), volatile)

        instructions = [aos.get_instruction()
                        for aos in agent_observed_state_list]
        instructions_batch = cuda_var(torch.from_numpy(np.array(instructions)).long())

        time = agent_observed_state.time_step
        time = cuda_var(torch.from_numpy(np.array([time])).long())

        probs_batch, new_model_state, image_emb_seq, state_feature = self.final_module(
            image_batch, instructions_batch, time, mode, model_state)
        return probs_batch, new_model_state, image_emb_seq, state_feature

3 View Complete Implementation : incremental_model_chaplot_resnet.py
Copyright GNU General Public License v3.0
Author : lil-lab
    def get_probs(self, agent_observed_state, model_state, mode=None, volatile=False):

        astert isinstance(agent_observed_state, AgentObservedState)
        agent_observed_state_list = [agent_observed_state]

        image_seqs = [[aos.get_last_image()]
                      for aos in agent_observed_state_list]
        image_batch = cuda_var(torch.from_numpy(np.array(image_seqs)).float(), volatile)

        instructions = [aos.get_instruction()
                        for aos in agent_observed_state_list]
        instructions_batch = cuda_var(torch.from_numpy(np.array(instructions)).long())

        time = agent_observed_state.time_step
        time = cuda_var(torch.from_numpy(np.array([time])).long())

        probs_batch, new_model_state, image_emb_seq, state_feature = self.final_module(
            image_batch, instructions_batch, time, mode, model_state)
        return probs_batch, new_model_state, image_emb_seq, state_feature

3 View Complete Implementation : incremental_model_chaplot.py
Copyright GNU General Public License v3.0
Author : lil-lab
    def get_probs(self, agent_observed_state, model_state, mode=None, volatile=False):

        astert isinstance(agent_observed_state, AgentObservedState)
        agent_observed_state_list = [agent_observed_state]

        image_seqs = [[aos.get_last_image()]
                      for aos in agent_observed_state_list]
        image_batch = cuda_var(torch.from_numpy(np.array(image_seqs)).float(), volatile)

        instructions = [aos.get_instruction()
                        for aos in agent_observed_state_list]
        instructions_batch = cuda_var(torch.from_numpy(np.array(instructions)).long())

        time = agent_observed_state.time_step
        time = cuda_var(torch.from_numpy(np.array([time])).long())

        probs_batch, new_model_state, image_emb_seq, state_feature = self.final_module(
            image_batch, instructions_batch, time, mode, model_state)
        return probs_batch, new_model_state, image_emb_seq, state_feature

3 View Complete Implementation : tmp_blocks_incremental_model_chaplot.py
Copyright GNU General Public License v3.0
Author : lil-lab
    def get_probs(self, agent_observed_state, model_state, mode=None, volatile=False):

        astert isinstance(agent_observed_state, AgentObservedState)
        agent_observed_state_list = [agent_observed_state]

        image_seqs = [[aos.get_last_image()]
                      for aos in agent_observed_state_list]
        image_batch = cuda_var(torch.from_numpy(np.array(image_seqs)).float(), volatile)

        instructions = [aos.get_instruction()
                        for aos in agent_observed_state_list]
        instructions_batch = cuda_var(torch.from_numpy(np.array(instructions)).long())

        time = agent_observed_state.time_step
        time = cuda_var(torch.from_numpy(np.array([time])).long())

        probs_batch, new_model_state, image_emb_seq, state_feature = self.final_module(
            image_batch, instructions_batch, time, mode, model_state)
        return probs_batch, new_model_state, image_emb_seq, state_feature

3 View Complete Implementation : tmp_house_incremental_model_chaplot.py
Copyright GNU General Public License v3.0
Author : lil-lab
    def get_probs(self, agent_observed_state, model_state, mode=None, volatile=False):

        astert isinstance(agent_observed_state, AgentObservedState)
        agent_observed_state_list = [agent_observed_state]

        image_seqs = [[aos.get_last_image()]
                      for aos in agent_observed_state_list]
        image_batch = cuda_var(torch.from_numpy(np.array(image_seqs)).float(), volatile)

        instructions = [aos.get_instruction()
                        for aos in agent_observed_state_list]
        instructions_batch = cuda_var(torch.from_numpy(np.array(instructions)).long())

        time = agent_observed_state.time_step
        time = cuda_var(torch.from_numpy(np.array([time])).long())

        probs_batch, new_model_state, image_emb_seq, state_feature = self.final_module(
            image_batch, instructions_batch, time, mode, model_state)
        return probs_batch, new_model_state, image_emb_seq, state_feature

3 View Complete Implementation : tmp_streetview_incremental_model_recurrent_policy_network.py
Copyright GNU General Public License v3.0
Author : lil-lab
    def get_probs(self, agent_observed_state, model_state, mode=None, volatile=False):

        astert isinstance(agent_observed_state, AgentObservedState)
        agent_observed_state_list = [agent_observed_state]

        image_seqs = [[aos.get_last_image()]
                      for aos in agent_observed_state_list]
        image_batch = cuda_var(torch.from_numpy(np.array(image_seqs)).float(), volatile)

        instructions = [aos.get_instruction()
                        for aos in agent_observed_state_list]
        instructions_batch = cuda_var(torch.from_numpy(np.array(instructions)).long())

        time = agent_observed_state.time_step
        time = cuda_var(torch.from_numpy(np.array([time])).long())

        probs_batch, new_model_state, image_emb_seq, state_feature = self.final_module(
            image_batch, instructions_batch, time, mode, model_state)
        return probs_batch, new_model_state, image_emb_seq, state_feature

3 View Complete Implementation : bilinear_test.py
Copyright Apache License 2.0
Author : UKPLab
    def test_forward_works_with_higher_order_tensors(self):
        # pylint: disable=protected-access
        bilinear = BilinearSimilarity(4, 7)
        weights = numpy.random.rand(4, 7)
        bilinear._weight_matrix = Parameter(torch.from_numpy(weights).float())
        bilinear._bias = Parameter(torch.from_numpy(numpy.asarray([0])).float())
        a_vectors = numpy.random.rand(5, 4, 3, 6, 4)
        b_vectors = numpy.random.rand(5, 4, 3, 6, 7)
        a_variables = torch.from_numpy(a_vectors).float()
        b_variables = torch.from_numpy(b_vectors).float()
        result = bilinear(a_variables, b_variables).data.numpy()
        astert result.shape == (5, 4, 3, 6)
        expected_result = numpy.dot(numpy.dot(numpy.transpose(a_vectors[3, 2, 1, 3]), weights),
                                    b_vectors[3, 2, 1, 3])
        astert_almost_equal(result[3, 2, 1, 3], expected_result, decimal=5)

3 View Complete Implementation : intra_sentence_attention_test.py
Copyright Apache License 2.0
Author : UKPLab
    def test_forward_works_with_multi_headed_attention(self):
        # We're not going to check the output values here, as that's complicated; we'll just make
        # sure the code runs and the shapes are correct.
        similarity = MultiHeadedSimilarity(3, 24)
        encoder = IntraSentenceAttentionEncoder(input_dim=24,
                                                projection_dim=24,
                                                similarity_function=similarity,
                                                num_attention_heads=3,
                                                combination="1+2")
        input_tensor = torch.from_numpy(numpy.random.rand(4, 6, 24)).float()
        encoder_output = encoder(input_tensor, None)
        astert list(encoder_output.size()) == [4, 6, 24]

3 View Complete Implementation : util_test.py
Copyright Apache License 2.0
Author : UKPLab
    def test_weighted_sum_works_on_simple_input(self):
        batch_size = 1
        sentence_length = 5
        embedding_dim = 4
        sentence_array = numpy.random.rand(batch_size, sentence_length, embedding_dim)
        sentence_tensor = torch.from_numpy(sentence_array).float()
        attention_tensor = torch.FloatTensor([[.3, .4, .1, 0, 1.2]])
        aggregated_array = util.weighted_sum(sentence_tensor, attention_tensor).data.numpy()
        astert aggregated_array.shape == (batch_size, embedding_dim)
        expected_array = (0.3 * sentence_array[0, 0] +
                          0.4 * sentence_array[0, 1] +
                          0.1 * sentence_array[0, 2] +
                          0.0 * sentence_array[0, 3] +
                          1.2 * sentence_array[0, 4])
        numpy.testing.astert_almost_equal(aggregated_array, [expected_array], decimal=5)

3 View Complete Implementation : util_test.py
Copyright Apache License 2.0
Author : UKPLab
    def test_weighted_sum_works_on_simple_input(self):
        batch_size = 1
        sentence_length = 5
        embedding_dim = 4
        sentence_array = numpy.random.rand(batch_size, sentence_length, embedding_dim)
        sentence_tensor = torch.from_numpy(sentence_array).float()
        attention_tensor = torch.FloatTensor([[.3, .4, .1, 0, 1.2]])
        aggregated_array = util.weighted_sum(sentence_tensor, attention_tensor).data.numpy()
        astert aggregated_array.shape == (batch_size, embedding_dim)
        expected_array = (0.3 * sentence_array[0, 0] +
                          0.4 * sentence_array[0, 1] +
                          0.1 * sentence_array[0, 2] +
                          0.0 * sentence_array[0, 3] +
                          1.2 * sentence_array[0, 4])
        numpy.testing.astert_almost_equal(aggregated_array, [expected_array], decimal=5)