Tensorflow 是 Google 提供的机器学习框架。它是一个与 Python 结合使用以实现算法、深度学习应用程序等的开源框架。它用于研究和生产目的。
可以使用以下代码行在 Windows 上安装“tensorflow”包 -
pip install tensorflow
Tensor 是 TensorFlow 中使用的一种数据结构。它有助于连接流程图中的边。该流程图被称为“数据流图”。张量只不过是一个多维数组或列表。
Keras 是一个深度学习 API,它是用 Python 编写的。它是一种高级 API,具有有助于解决机器学习问题的高效界面。它运行在 Tensorflow 框架之上。它旨在帮助快速进行实验。它提供了在开发和封装机器学习解决方案中必不可少的基本抽象和构建块。
Keras 已经存在于 Tensorflow 包中。可以使用以下代码行访问它。
import tensorflow from tensorflow import keras
与使用顺序 API 创建的模型相比,Keras 函数式 API 有助于创建更灵活的模型。函数式 API 可以处理具有非线性拓扑结构的模型,可以共享层并处理多个输入和输出。深度学习模型通常是包含多个层的有向无环图 (DAG)。函数式 API 有助于构建层图。
我们正在使用 Google Colaboratory 运行以下代码。Google Colab 或 Colaboratory 帮助在浏览器上运行 Python 代码,并且需要零配置和免费访问 GPU(图形处理单元)。Colaboratory 建立在 Jupyter Notebook 之上。以下是代码片段 -
class CustomDense(layers.Layer): def __init__(self, units=32): super(CustomDense, self).__init__() self.units= units def build(self, input_shape): self.w= self.add_weight( shape=(input_shape[-1], self.units), initializer="random_normal", trainable=True, ) self.b= self.add_weight( shape=(self.units,), initializer="random_normal", trainable=True ) def call(self, inputs): return tf.matmul(inputs, self.w) + self.b inputs = keras.Input((4,)) outputs = CustomDense(10)(inputs) print("Keras model is being generated") model = keras.Model(inputs, outputs)
代码信用 - https://www.tensorflow.org/guide/keras/functional
输出结果
Keras model is being generated
Keras 带有多个内置层,其中一些包括“Conv1D”、“Conv2D”、“Conv2DTranspose”等。
'call' 方法指定由层执行的计算。
'build' 方法为层创建权重。
模型已生成。