GVKun编程网logo

Swift for TensorFlow 0.3.1 发布,专为 TensorFlow 设计的编程模型(tensorflow2.0 helloworld)

4

针对SwiftforTensorFlow0.3.1发布,专为TensorFlow设计的编程模型和tensorflow2.0helloworld这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展

针对Swift for TensorFlow 0.3.1 发布,专为 TensorFlow 设计的编程模型tensorflow2.0 helloworld这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展github/tensorflow/tensorflow/contrib/slim/、Swift 新手,无法手动将 swift 2 转换为 swift 5、Swift 无法从 Swift.X 转换为 Swift.X.Type、Swift! Swift! Swift! 重要的事情说3遍!等相关知识,希望可以帮助到你。

本文目录一览:

Swift for TensorFlow 0.3.1 发布,专为 TensorFlow 设计的编程模型(tensorflow2.0 helloworld)

Swift for TensorFlow 0.3.1 发布,专为 TensorFlow 设计的编程模型(tensorflow2.0 helloworld)

Swift for TensorFlow 0.3.1 发布了。Swift for TensorFlow 由 Google 开源,为 TensorFlow 提供了一种新的编程模型。它将 TensorFlow 计算图与 Eager Execution 的灵活性和表达能力结合在了一起,同时还注重提高整个软件架构每一层的可用性。

新版更新亮点有:

在 Jupyter 和 Colab 安装 Swift 包

新版本可用 %install 指令在笔记本上直接安装 Swift Package Manager packages。指令既适用于 GitHub 的包,也适用于本地文件系统上的包。 (%install 指令的更多说明)

深度学习库和 Tensor 的增强

  • 深度学习库的 API 得到更新和完善

  • Tensor pretty printing (swift#23837)

  • TensorFlow API 更改为使用 Int,而不是 Int 32

数组可微分

现在 Array(数组)遵循可微分协议。不久后控制流也将支持,从而实现 for 循环函数的可微分。

发布公告

github/tensorflow/tensorflow/contrib/slim/

github/tensorflow/tensorflow/contrib/slim/

TensorFlow-Slim

TF-Slim 是一个轻量级的库,用来在TF中定义、训练和评估复杂模型。tf-slim能够自由混入原生TF和其它框架(如tf.contrib.learn中)。

用法

import tensorflow.contrib.slim as slim

为什么用TF-Slim?

TF-Slim中都有什么组成部分?

定义模型

变量

Scopes

实例: 实现VGG16

训练模型

Training Tensorflow models requires a model, a loss function, the gradient computation and a training routine that iteratively computes the gradients of the model weights relative to the loss and updates the weights accordingly. TF-Slim provides both common loss functions and a set of helper functions that run the training and evaluation routines.

损失

The loss function defines a quantity that we want to minimize. For classification problems, this is typically the cross entropy between the true distribution and the predicted probability distribution across classes. For regression problems, this is often the sum-of-squares differences between the predicted and true values.

Certain models, such as multi-task learning models, require the use of multiple loss functions simultaneously. In other words, the loss function ultimately being minimized is the sum of varIoUs other loss functions. For example, consider a model that predicts both the type of scene in an image as well as the depth from the camera of each pixel. This model's loss function would be the sum of the classification loss and depth prediction loss.

TF-Slim provides an easy-to-use mechanism for defining and keeping track of loss functions via the losses module. Consider the simple case where we want to train the VGG network:

Training Loop

TF-Slim provides a simple but powerful set of tools for training models found in learning.py. These include a Train function that repeatedly measures the loss, computes gradients and saves the model to disk, as well as several convenience functions for manipulating gradients. For example, once we've specified the model, the loss function and the optimization scheme, we can call slim.learning.create_train_op and slim.learning.train to perform the optimization:

实例: 训练VGG16模型

To illustrate this, let's examine the following sample of training the VGG network:

微调已存在的模型

 

Brief Recap on Restoring Variables from a Checkpoint

After a model has been trained, it can be restored using tf.train.Saver() which restores Variables from a given checkpoint. For many cases, tf.train.Saver() provides a simple mechanism to restore all or just a few variables.

 

Partially Restoring Models

It is often desirable to fine-tune a pre-trained model on an entirely new dataset or even a new task. In these situations, one can use TF-Slim's helper functions to select a subset of variables to restore:

 

Restoring models with different variable names

 

Fine-Tuning a Model on a different task

Consider the case where we have a pre-trained VGG16 model. The model was trained on the ImageNet dataset, which has 1000 classes. However, we would like to apply it to the Pascal VOC dataset which has only 20 classes. To do so, we can initialize our new model using the values of the pre-trained model excluding the final layer:

 

评估模型

Once we've trained a model (or even while the model is busy training) we'd like to see how well the model performs in practice. This is accomplished by picking a set of evaluation metrics, which will grade the model's performance, and the evaluation code which actually loads the data, performs inference, compares the results to the ground truth and records the evaluation scores. This step may be performed once or repeated periodically.

 

度量

我们定义一个度量来衡量训练效果,这不是一个损失函数(损失被用来在训练过程中进行优化的)。例如,我们训练时最小化log损失,但是评估模型时我们也许会用 F1 score ,或者 Intersection Over Union score(这个值不可微,因此也不能用在损失函数上)。

TF-Slim提供了一组度量 operations。笼统地讲,计算一个度量值可以被分为三部分:

  1. 初始化:初始化用来计算度量的变量。
  2. Aggregation: perform operations (sums, etc) used to compute the metrics.
  3. Finalization: (optionally) perform any final operation to compute metric values. For example, computing means, mins, maxes, etc.

例如,计算mean_absolute_error,两个变量 (counttotal)被初始化为0。在 aggregation,我们得到一组predictions 和 labels,计算它们的绝对误差并总计为total。我们每增加一组,count也随之增加。最后,在 finalization阶段 ,total除以count来获得均值。

The following example demonstrates the API for declaring metrics. Because metrics are often evaluated on a test set which is different from the training set (upon which the loss is computed), we'll assume we're using test data:

images, labels = LoadTestData(...)
predictions = MyModel(images)

mae_value_op, mae_update_op = slim.metrics.streaming_mean_absolute_error(predictions, labels)
mre_value_op, mre_update_op = slim.metrics.streaming_mean_relative_error(predictions, labels)
pl_value_op, pl_update_op = slim.metrics.percentage_less(mean_relative_errors, 0.3)

就像例子描述的那样,创建的metric返回两个值: value_opupdate_op。 value_op是一个 idempotent operation 返回metric的当前值。update_op 是一个 operation,它执行 aggregation步骤 并返回metric的值。

跟踪value_opupdate_op 费时费力。为了解决这个问题,TF-Slim提供两个方便的函数:

# 总计value和update ops 到两个列表中:
value_ops, update_ops = slim.metrics.aggregate_metrics(
    slim.metrics.streaming_mean_absolute_error(predictions, labels),
    slim.metrics.streaming_mean_squared_error(predictions, labels))

# 总起value和update ops 到两个字典中:
names_to_values, names_to_updates = slim.metrics.aggregate_metric_map({
    "eval/mean_absolute_error": slim.metrics.streaming_mean_absolute_error(predictions, labels),
    "eval/mean_squared_error": slim.metrics.streaming_mean_squared_error(predictions, labels),
})

 

Swift 新手,无法手动将 swift 2 转换为 swift 5

Swift 新手,无法手动将 swift 2 转换为 swift 5

如何解决Swift 新手,无法手动将 swift 2 转换为 swift 5

我年轻的时候经常搞砸,我构建了一个应用程序,我目前正在尝试修复并改进该应用程序,但是我在将 swift 2 转换为 swift 5 时遇到了一些问题,并且该应用程序不会成功编译

问题 1: 无法将 ''(NSError) -> ()'' 类型的值转换为预期的参数类型 ''((Error) -> Void)?''

参考这两行代码:

  1. }) { (error:NSError) in
  2. print(error.localizedDescription)

问题 2: Any 类型的值没有下标

参考这些行:

  1. key = snapshot.key
  2. itemRef = snapshot.ref
  3. if let shareContent = snapshot.value!["content"] as? [[String:Any]] {
  4. content = shareContent
  5. }
  6. else{
  7. content = ""
  8. }
  9. if let shareUser = snapshot.value!["addedByUser"] as? [[String:Any]] {
  10. addedByUser = shareUser
  11. }else{
  12. content = ""

问题 3: 表达类型不明确,没有更多上下文

  1. FIRAuth.auth()?.signInWithEmail("",password: "",completion: { (user:FIRUser?,error:NSError?) in
  2. if error == nil {
  3. print(user?.email)

如果有人可以帮助其中任何一个,我将非常感激

解决方法

问题 1) 和 3):不要注释类型,在 Swift 3+ 中,错误已成为符合 Error 的类型

  1. }) { error in
  2. print(error.localizedDescription)

关于 3) 在 Firebase 文档中查找正确的类型,它不再是 (FIRUser?,NSError?)

问题 2):在 Swift 3+ 中,编译器必须知道任何下标对象的静态类型。如果 value 应该是字典,你必须有条件地向下转换它

  1. if let sharedValue = snapshot.value as? [String:Any],let shareContent = sharedValue["content"] as? [[String:Any]] {
  2. content = shareContent
  3. }

Swift 无法从 Swift.X 转换为 Swift.X.Type

Swift 无法从 Swift.X 转换为 Swift.X.Type

你得到的错误

编译器给了我上面的例子“无法转换''Int32.Type类型的值?''到指定类型 ''Int32''"

是因为您在 Int32.self 中将 T 作为 KeyValuePairs 作为参数传递给您的函数,因此 T? 本身的返回类型变为 Int32.Type? .因此,当您尝试将其分配给 x 类型的 Int32 时,编译器会抛出错误,因为转换将失败。

所以为了解决这个问题,你可以更喜欢下面的代码:

数组扩展:

extension Array {
    func AtIndex(index: Int) -> Any {
        return self[index];
    }

    func AtIndexT<T>(index: KeyValuePairs<Int,T.Type>) -> T? {
        if let _record = index.first {
            let _key : T = self[_record.key] as! T;
            return _key;        
        } else {
            return nil;
        }
    }
}

用法:

let x:Int32? = Int(db.valueByKey(key:["integer_tgc":Int32.self]).AtIndexT(index: [2:Int32.self]))

注意: xOptional Int32 类型。

Swift! Swift! Swift! 重要的事情说3遍!

Swift! Swift! Swift! 重要的事情说3遍!

Swift正式开源,支持的平台包括Linux。


跟踪学习记录

关于Swift for TensorFlow 0.3.1 发布,专为 TensorFlow 设计的编程模型tensorflow2.0 helloworld的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于github/tensorflow/tensorflow/contrib/slim/、Swift 新手,无法手动将 swift 2 转换为 swift 5、Swift 无法从 Swift.X 转换为 Swift.X.Type、Swift! Swift! Swift! 重要的事情说3遍!的相关信息,请在本站寻找。

本文标签: