学习记录-12.27

python os.path() 模块

菜鸟教程

os.path 模块主要用于获取文件的属性。

os.path.isfile(path) 判断路径是否为文件
os.path.join(path1[, path2[, ...]]) 把目录和文件名合成一个路径

python pickle格式保存字典

使用 pickle 模块dump() 函数来保存字典,并使用load() 函数从保存的文件中读取字典。pickle 模块的 dump() 函数需要我们要保存的字典和文件对象作为参数,以便将字典保存为.pkl 文件

import pickle

my_dict = { 'Apple': 4, 'Banana': 2, 'Orange': 6, 'Grapes': 11}
with open("myDictionary.pkl", "wb") as tf:
    pickle.dump(my_dict,tf)

with open("myDictionary.pkl", "wb") as tf:
    new_dict = pickle.load(tf)

print(new_dict.item())

Pytorch数据加载—Dataset和DataLoader详解

学习

python map函数

map() 函数会根据提供的函数对指定序列做映射。

第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 function 函数返回值的新列表。

语法:map(function, iterable, ...)

  • function – 函数
  • iterable – 一个或多个序列
  • 返回一个迭代器
    在这里插入图片描述

GRU(Gate Recurrent Unit)

torch or keras如何指定GPU运行代码

参考链接

使用命令行CUDA_VISIBLE_DEVICES=0,1,2,3 python xxx.py 来设置该程序可见的gpu

代码内可以在开头添加:

import os 
os.environ['CUDA_VISIBLE_DEVICES']='1'

其他就是在写代码的时候,在需要的时候比如模型或者输入数据,后面加

net = Net.cuda(0)

torch.cuda.set_device(0) #在生成网络对象之前执行

keras model.fit()参数

参考链接

fit(x=None, y=None, batch_size=None, epochs=1, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0, steps_per_epoch=None, validation_steps=None, validation_freq=1, max_queue_size=10, workers=1, use_multiprocessing=False)

keras model class官网详解

官网链接

tf.keras.Model() Model 将层分组到具有训练和推理特征的对象中

参数

  • inputs:keras.Input 对象或 keras.Input 对象列表
  • outputs:
  • name:String,模型的名称
实例化Model–使用 Functional API

从输入开始,链接层调用以指定模型的前向传递,最后根据输入和输出创建模型:

import tensorflow as tf

inputs = tf.keras.Input(shape=(3,))
x = tf.keras.layers.Dense(4, activation=tf.nn.relu)(inputs)
outputs = tf.keras.layers.Dense(5, activation=tf.nn.softmax)(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)

注意:只支持字典、列表和输入张量的元组。不支持嵌套输入

还可以使用中间张量创建新的 Functional API,可以快速提取模型的子组件

inputs = keras.Input(shape=(None, None, 3))
processed = keras.layers.RandomCrop(width=32, height=32)(inputs)
conv = keras.layers.Conv2D(filters=2, kernel_size=3)(processed)
pooling = keras.layers.GlobalAveragePooling2D()(conv)
feature = keras.layers.Dense(10)(pooling)

full_model = keras.Model(inputs, feature)
backbone = keras.Model(processed, conv)
activations = keras.Model(conv, feature)

backboneactivations 不是使用 keras.Input 对象创建的,而是使用源自 keras.Inputs 对象的张量创建的。

在幕后,层和权重将在这些模型之间共享,以便用户可以训练 full_model,并使用 backboneactivations 来进行特征提取。

模型的输入和输出也可以是张量的嵌套结构,创建的模型是标准的函数式 API 模型,支持所有现有的 API。

实例化Model–通过子类化Model类

在这种情况下,应该在__init__()中定义你的层,并在call() 中实现模型的前向传递。

import tensorflow as tf

class MyModel(tf.keras.Model):

  def __init__(self):
    super().__init__()
    self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu)
    self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax)

  def call(self, inputs):
    x = self.dense1(inputs)
    return self.dense2(x)

model = MyModel()

如果对 Model 进行子类化,可以选择在 call() 中有一个train参数(布尔值),可以使用它来指定训练和推理中的不同行为:

import tensorflow as tf

class MyModel(tf.keras.Model):

  def __init__(self):
    super().__init__()
    self.dense1 = tf.keras.layers.Dense(4, activation=tf.nn.relu)
    self.dense2 = tf.keras.layers.Dense(5, activation=tf.nn.softmax)
    self.dropout = tf.keras.layers.Dropout(0.5)

  def call(self, inputs, training=False):
    x = self.dense1(inputs)
    if training:
      x = self.dropout(x, training=training)
    return self.dense2(x)

model = MyModel()

一旦创建了模型,就可以使用model.compile()配置模型的损失和度量,使用model.fit()训练模型,或者使用model.predict()使用模型进行预测。

keras.Input object

官网链接

tf.keras.Input(
    shape=None,
    batch_size=None,
    name=None,
    dtype=None,
    sparse=None,
    tensor=None,
    ragged=None,
    type_spec=None,
    **kwargs
)
summary method

打印网络的摘要

Model.summary(line_length=None, positions=None, print_fn=None, expand_nested=False)

ValueError:如果summary()在模型构建之前被调用

get_layer method

根据其名称(唯一)或索引检索层, 如果同时提供了name和index,则索引优先。索引基于水平图遍历的顺序(自底向上)

Model.get_layer(name=None, index=None)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值