def testToFloat(self):
        with self.test_session():
            input_string = array_ops.placeholder(dtypes.string)
            output = parsing_ops.string_to_number(input_string,
                                                  out_type=dtypes.float32)

            result = output.eval(
                feed_dict={
                    input_string: [
                        "0",
                        "3",
                        "-1",
                        "1.12",
                        "0xF",
                        "   -10.5",
                        "3.40282e+38",
                        # The next two exceed maximum value for float, so we
                        # expect +/-INF to be returned instead.
                        "3.40283e+38",
                        "-3.40283e+38",
                        "NAN",
                        "INF"
                    ]
                })

            self.assertAllClose([
                0, 3, -1, 1.12, 0xF, -10.5, 3.40282e+38,
                float("INF"),
                float("-INF"),
                float("NAN"),
                float("INF")
            ], result)

            with self.assertRaisesOpError(_ERROR_MESSAGE + "10foobar"):
                output.eval(feed_dict={input_string: ["10foobar"]})
  def testToFloat(self):
    with self.test_session():
      input_string = array_ops.placeholder(dtypes.string)
      output = parsing_ops.string_to_number(
          input_string, out_type=dtypes.float32)

      result = output.eval(feed_dict={
          input_string: [
              "0",
              "3",
              "-1",
              "1.12",
              "0xF",
              "   -10.5",
              "3.40282e+38",
              # The next two exceed maximum value for float, so we
              # expect +/-INF to be returned instead.
              "3.40283e+38",
              "-3.40283e+38",
              "NAN",
              "INF"
          ]
      })

      self.assertAllClose([
          0, 3, -1, 1.12, 0xF, -10.5, 3.40282e+38, float("INF"), float("-INF"),
          float("NAN"), float("INF")
      ], result)

      with self.assertRaisesOpError(_ERROR_MESSAGE + "10foobar"):
        output.eval(feed_dict={input_string: ["10foobar"]})
Exemplo n.º 3
0
    def _test(self, tf_type, good_pairs, bad_pairs):
        with self.cached_session():
            # Build a small testing graph.
            input_string = array_ops.placeholder(dtypes.string)
            output = parsing_ops.string_to_number(input_string,
                                                  out_type=tf_type)

            # Check all the good input/output pairs.
            for instr, outnum in good_pairs:
                result, = output.eval(feed_dict={input_string: [instr]})
                self.assertAllClose([outnum], [result])

            # Check that the bad inputs produce the right errors.
            for instr, outstr in bad_pairs:
                with self.assertRaisesOpError(outstr):
                    output.eval(feed_dict={input_string: [instr]})
  def _test(self, tf_type, good_pairs, bad_pairs):
    with self.test_session():
      # Build a small testing graph.
      input_string = array_ops.placeholder(dtypes.string)
      output = parsing_ops.string_to_number(
          input_string, out_type=tf_type)

      # Check all the good input/output pairs.
      for instr, outnum in good_pairs:
        result, = output.eval(feed_dict={input_string: [instr]})
        self.assertAllClose([outnum], [result])

      # Check that the bad inputs produce the right errors.
      for instr, outstr in bad_pairs:
        with self.assertRaisesOpError(outstr):
          output.eval(feed_dict={input_string: [instr]})
  def testToInt32(self):
    with self.test_session():
      input_string = array_ops.placeholder(dtypes.string)
      output = parsing_ops.string_to_number(input_string, out_type=dtypes.int32)

      result = output.eval(feed_dict={
          input_string:
              ["0", "3", "-1", "    -10", "-2147483648", "2147483647"]
      })

      self.assertAllEqual([0, 3, -1, -10, -2147483648, 2147483647], result)

      with self.assertRaisesOpError(_ERROR_MESSAGE + "2.9"):
        output.eval(feed_dict={input_string: ["2.9"]})

      # The next two exceed maximum value of int32.
      for in_string in ["-2147483649", "2147483648"]:
        with self.assertRaisesOpError(_ERROR_MESSAGE + in_string):
          output.eval(feed_dict={input_string: [in_string]})
    def testToInt32(self):
        with self.test_session():
            input_string = array_ops.placeholder(dtypes.string)
            output = parsing_ops.string_to_number(input_string,
                                                  out_type=dtypes.int32)

            result = output.eval(
                feed_dict={
                    input_string:
                    ["0", "3", "-1", "    -10", "-2147483648", "2147483647"]
                })

            self.assertAllEqual([0, 3, -1, -10, -2147483648, 2147483647],
                                result)

            with self.assertRaisesOpError(_ERROR_MESSAGE + "2.9"):
                output.eval(feed_dict={input_string: ["2.9"]})

            # The next two exceed maximum value of int32.
            for in_string in ["-2147483649", "2147483648"]:
                with self.assertRaisesOpError(_ERROR_MESSAGE + in_string):
                    output.eval(feed_dict={input_string: [in_string]})