如何将tensorflow的float32转换为python的float类型

时间:2018-12-28 11:08:11

标签: python tensorflow

我有一个tensorflow的2D数组,其一维包含float32类型的数据。我需要将其转换为基于Python的float,以便对其进行序列化。

"""Runs the audio data through the graph and prints predictions."""
  with tf.Session() as sess:
    # Feed the audio data as input to the graph.
    #   predictions  will contain a two-dimensional array, where one
    #   dimension represents the input image count, and the other has
    #   predictions per class
    softmax_tensor = sess.graph.get_tensor_by_name(output_layer_name)
    predictions, = sess.run(softmax_tensor, {input_layer_name: wav_data})

    # Sort to show labels in order of confidence
    top_k = predictions.argsort()[-num_top_predictions:][::-1]
    result = {}
    for node_id in top_k:
      human_string = labels[node_id]
      score = predictions[node_id] ## Currently a float32 type.
      result[human_string] = score

    return result

当前,当我尝试序列化字典时,出现以下异常:

TypeError: Object of type float32 is not JSON serializable 

0 个答案:

没有答案