编程语言
首页 > 编程语言> > 吴恩达深度学习编程作业第四周第一节——Convolution model

吴恩达深度学习编程作业第四周第一节——Convolution model

作者:互联网

本次练习使用的是tensorflow 2.6版本,也就是最新版。但作业中很多调用语句都是老版的,导致出现很多模块引用报错。
解决办法主要有两种:1.降低tensorflow版本; 2.修改引用语句
1.Tensorflow 模型
数据集中的图片已经进行了标注,index 用于索引不同的图片,可以自行修改查看不同结果:
在这里插入图片描述

# Example of a picture
index = 1
plt.imshow(X_train_orig[index])
print ("y = " + str(np.squeeze(Y_train_orig[:, index])))

index=1
1.1创建占位符
报错1:

AttributeError:'tensorflow' has no attribute 'palceholder'

解决方法:
修改引用方式

tf.compat.v1.disable_eager_execution()

修改使用方式

X = tf.compat.v1.placeholder(tf.float32, shape=[None, n_H0, n_W0, n_C0])

1.2 初始化参数
初始化权重和过滤器,无需担心偏差变量,tf 会自动解决。tf 会为了全连接自动初始化这些层。
报错2:
在对参数W1,W2进行初始化时

W1 = tf.get_variable("W1", [4, 4, 3, 8], initializer=tf.contrib.layers.xavier_initializer(seed=0))
AttributeError: module 'tensorflow' has no attribute 'get_variable'

又一次因为版本问题报错,在tensorflow2.x 版本中已经将原有的tf.contrib弃用,需要使用其他函数表示,修改程序如下:

W1 = tf.compat.v1.get_variable("W1", [4, 4, 3, 8], initializer=tf.keras.initializers.GlorotNormal(seed = 0))

1.3 前向传播
报错1:

AttributeError: 'Tensor' object has no attribute 'lower'

1.4 计算成本
用到的两个函数:
分别是交叉熵函数和成本求和函数

tf.nn.softmax_cross_entropy_with_logits(logits = Z3, labels = Y)
tf.compat.v1.reduce_mean()

1.5 模型
报错:

AttributeError: module 'scipy.ndimage' has no attribute 'imread'

解决方法:

plt.imread(fname)

最后阶段出现很多使用scipy 出现的引用问题,实际上也是版本更新所致,一些函数已经被弃用。下面简单总结一下scipy在不降版本的前提下,如何调用常见的函数:
本次使用到的imread, imresize

import imageio
img = imageio.imread(image_path)

from skimage.transform import resize
scaled_temp = resize(cropped_temp,output_shape=(image_size, image_size))

imsave

import imageio
imageio.imwrite(output_filename,scaled_temp)

Once again, here's a thumbs up for your work!

标签:compat,index,吴恩达,Convolution,no,报错,W1,tf,model
来源: https://blog.csdn.net/weix1235/article/details/120633816