In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail what Swift is used for, Xiaobian thinks it is quite practical, so share it with you for a reference, I hope you can gain something after reading this article.
1. Why Swift?
PyTorch is designed to overcome limitations in Tensorflow. But now we're approaching Python's limits, and Swift has the potential to fill that gap. "--Jeremy Howard
Swift has recently attracted a lot of excitement and attention as a data science language. Everyone is talking about it. Here are a few reasons why you should learn Swift:
Swift is fast, very close to C speed.
At the same time, it has a very simple and readable syntax, very similar to Python:
Swift is a more efficient, stable and secure programming language than Python.
It is also a good mobile language. In fact, it's the official language for developing iOS apps for iPhone.
It has strong integrated support for automatic differentiation, making it one of the few high-level languages for numerical computation.
It's backed by Google, Apple, FastAI and more!
Jeremy Howard's comments on Swift: https://youtu.be/drSpCwDFwnM
2. Swift Foundation for Data Analysis
Before we get into the details of performing data science with Swift, let's briefly cover the basics of the Swift programming language.
2.1 Swift's ecosystem
Swift for current data science consists primarily of two ecosystems:
open source ecosystem
Apple's ecosystem
The open source ecosystem is where we can download and run Swift on any operating system or machine. We can use very cool Swift libraries to build machine learning applications, such as Swift, SwiftAI and SwiftPlot for TensorFlow.
Swift also allows us to seamlessly import mature data science libraries from Python, such as NumPy, panda, matplotlib, and scikit-learn.
Apple's ecosystem, on the other hand, is impressive in itself. There are useful libraries, such as CoreML, that allow us to train large models in Python and import them directly into Swift for inference. In addition, it provides a large number of pre-trained advanced models that we can use directly to build iOS/macOS applications.
There are other interesting libraries like swift , corem,transformer that allow us to run the most advanced text generation models on iPhone like GPT-2, BERT, etc.
When you need to build machine learning-based applications for Apple devices, there are many other libraries that provide good functionality.
There are many differences between these two ecosystems. But most importantly, in order to use Apple's ecosystem, you need to have an Apple machine, and you can only develop for Apple devices such as iOS, macOS, etc.
Now that you have an overview of Swift as a data science language, let's get into the code!
2.2 Setting up the environment for Swift
Swift is available for Google Colab with GPU and TPU versions. We will use it so that you can quickly keep up with it without having to spend too much time during the installation process.
You can open a Colab notebook by following these steps, which are quick to activate:
Open a blank Swift notebook (https://colab.research.google.com/github/tensorflow/swift/blob/master/notebooks/blank_swift.ipynb)
Click on "File" and select "Save a copy in Drive"-this will save a new Swift notebook on your own Google Drive!
I am ready to start writing Swift code. Here is the first line:
If you want to use Swift on your own system, here are some links you can open:
If you want to install Swift locally, you can follow the installation instructions: https://swift.org/getting-started/#using-the-repl
To install Jupyter notebooks on Ubuntu, please refer to Jeremy Howard's instructions for installing Swift: https://forums.fast.ai/t/jeremys-harebrained-install-guide/43814
On Ubuntu, you can also install Swift with Docker: https://github.com/apple/swift-docker
Now, let's take a quick look at some basic Swift functions before moving on to data science functionality that uses it.
2.3 printing function
I believe you used this. It works very similar to Python. Just call print() and enter what you want to print in parentheses:
2.4 Swift variables
Swift provides two useful options for creating variables:let and var. let is used to create a constant whose value cannot be changed anywhere in the program. var is very similar to variables we see in Python-you can change the values stored in it at any time in your program.
Let's look at an example to see the difference. Create two variables a and b:
Now, try changing the values of a and b:
You'll notice that b can update its value without reporting an error, while a gives an error:
This ability to create constants and variables is useful to help prevent invisible bugs from appearing in our code. As you'll see further in this article, we'll use let to create constants that store important information and don't need to change values,
Here's a tip: Use var to create results that you want to use some intermediate calculations because those intermediate calculations need to change. Similarly, let is used to store training data or results, which are basically values you don't want to change or mess with.
Swift also has a cool feature where you can even use emojis as variable names!
This is because Swift supports Unicode so much that we can create variables with Greek letters:
2.5 Swift data types
Swift supports all common data types such as integers, strings, floating point numbers, and doubles. We can assign to any variable whose type Swift automatically detects:
You can also explicitly write data types when creating variables. This helps prevent errors in the program because if the types don't match. Swift will throw an error:
You can take a quiz. Create a constant of explicit type "Float" with a value of 4, which results in an error.
A simple way to include the value of a variable in a string is to enclose the variable in parentheses and write a backslash () before the parentheses. For example:
You can use three double quotes ("") for strings that take up multiple lines.
2.6 lists and dictionaries
Swift supports list and dictionary data structures, just like Python (another comparison!) Unlike Python, we don't need separate syntax like "{}" for dictionaries and "[]" for lists.
Let's create a list and dictionary in Swift:
We can access the elements of a list or dictionary by writing an index or key inside "[]" brackets (similar to Python):
The above code adds the key-value pair of "Jayne" and "Public Relations" to the dictionary. If you print the dictionary above, the output is as follows:
2.7 usage cycle
Loops are one of the most important features of any programming language, and Swift won't let you down. Not only does it support all the traditional loop mechanisms (for, while, etc.), but it also implements some variants of its own.
for.. in loop
Much like Python, you can use list or range in Swift to use the for loop:
The three dots in the first example represent "range" in Swift. If we want to do something in the range of a to b, we use the syntax of a…b.
Similarly, if we want to dispense with the last number, we can change the three dots to "..
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.