欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

写Tensorflow遇到的bug和常用Python方法总结

程序员文章站 2024-01-05 16:30:46
...

1. 问题:ValueError: Only call `softmax_cross_entropy_with_logits` with named arguments (labels=..., logits=..., ...)

解决办法:将tf.nn.softmax_cross_entropy_with_logits(y, out)改写为:

                     tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=out)

2.问题:ValueError: Cannot evaluate tensor using `eval()`

解决办法:将print(x.eval())改写为:
                     with tf.Session() as sess:
                            print(x.eval())

3. 问题:Input ‘split_dim’ of ‘Split’ Op has type float32 that does not match expected type of int32


解决办法:将hsplit = tf.split(0, nsteps, h)改写为:

                      hsplit = tf.split(h, nsteps, 0)

有关slip方法:

import tensorflow as tf

A = [[1, 2, 3], [4, 5, 6]]
x = tf.split(A, 3, 1)

with tf.Session() as sess:
    c = sess.run(x)
    for ele in c:
        print(ele)

打印效果如下:

[[1]
 [4]]
[[2]
 [5]]
[[3]
 [6]]

4.返回数组(List)长度的方法

array = [0,1,2,3,4,5]
print len(array)
6

即用:len(list),而不是用list.length

5. numpy库数组属性查看:类型、尺寸、形状、维度

import numpy as np  
  
a1 = np.array([1,2,3,4,5,6],dtype=np.float32)  
print(a1)  
print("数据类型",type(a1))           #打印数组数据类型  
print("数组元素数据类型:",a1.dtype) #打印数组元素数据类型  
print("数组元素总数:",a1.size)      #打印数组尺寸,即数组元素总数  
print("数组形状:",a1.shape)         #打印数组形状  
print("数组的维度数目",a1.ndim)      #打印数组的维度数目 

6.问题:AttributeError:_parse_flags

解决办法:将FLAGS._parse_flags()改写为:

                      FLAGS.flag_values_dict()

7.问题:TypeError: can't concat str to bytes

解决办法:将out.write(result.encode('utf-8').strip()+"\n") 更改为:

                     out.write(str(result.encode('utf-8').strip()+b"\n"))

8. 问题:TypeError: write() argument must be str, not bytes

解决办法:将filehandle = open(WAV_FILE, 'w')更改为:
                      filehandle = open(WAV_FILE, 'wb+')

未完待续!!

相关标签: 代码问题