markdown
stringlengths
0
37k
code
stringlengths
1
33.3k
path
stringlengths
8
215
repo_name
stringlengths
6
77
license
stringclasses
15 values
이 tff.federated_computation은 페더레이션 유형 <float>@CLIENTS의 인수를 받아들이고 페더레이션 유형 <float>@SERVER의 값을 반환합니다. 페더레이션 계산은 서버에서 클라이언트로, 클라이언트에서 클라이언트로 또는 서버에서 서버로 이동할 수도 있습니다. 페더레이션 계산은 유형 서명이 일치하는 한 일반 함수처럼 구성할 수도 있습니다. 개발을 지원하기 위해 TFF를 사용하면 tff.federated_computation을 Python 함수로 호출할 수 있습니다. 예를 들어 다음을 호출할 수 있습니다.
get_average_temperature([68.5, 70.3, 69.8])
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
비 즉시 실행 계산 및 TensorFlow 두 가지 주요 제한 사항을 알고 있어야 합니다. 첫째, Python 인터프리터가 tff.federated_computation 데코레이터를 만나면 함수가 한 번 추적되고 나중에 사용할 수 있도록 직렬화됩니다. 페더레이션 학습의 분산된 특성으로 인해 이러한 미래의 사용은 원격 실행 환경과 같은 다른 곳에서 발생할 수 있습니다. 따라서 TFF 계산은 기본적으로 비 즉시 실행입니다. 이 동작은 TensorFlow의 tf.function 데코레이터와 다소 유사합니다. 둘째, 페더레이션 계산은 페더레이션 연산자(예: tff.feder...
@tff.tf_computation(tf.float32) def add_half(x): return tf.add(x, 0.5)
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
또한 유형 서명이 있지만 배치는 없습니다 . 예를 들어 다음을 호출할 수 있습니다.
str(add_half.type_signature)
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
여기에서 tff.federated_computation과 tff.tf_computation의 중요한 차이점을 볼 수 있습니다. 전자는 명시적 배치가 있는 반면 후자는 그렇지 않습니다. 게재 위치를 지정하여 페더레이션 계산에서 tff.tf_computation 블록을 사용할 수 있습니다. 절반을 추가하지만 클라이언트에서 페더레이션 부동 소수점에만 추가하는 함수를 만들어 보겠습니다. 게재 위치를 유지하면서 지정된 tff.tf_computation을 적용하는 tff.federated_map을 사용하여 이를 수행할 수 있습니다.
@tff.federated_computation(tff.FederatedType(tf.float32, tff.CLIENTS)) def add_half_on_clients(x): return tff.federated_map(add_half, x)
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
이 함수는 tff.CLIENTS에 배치된 값만 허용하고 동일한 배치의 값을 반환한다는 점을 제외하면 add_half와 거의 동일합니다. 유형 서명에서 이를 확인할 수 있습니다.
str(add_half_on_clients.type_signature)
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
요약하자면: TFF는 페더레이션 값에서 작동합니다. 각 페더레이션 값에는 유형(예: tf.float32)과 배치(예: tff.CLIENTS)가 있는 페더레이션 유형{/em}이 있습니다. 페더레이션 값은 페더레이션 계산을 사용하여 변환될 수 있으며, tff.federated_computation 및 연합 유형 서명으로 데코레이션되어야 합니다. TensorFlow 코드는 tff.tf_computation 데코레이터가 있는 블록에 포함되어야 합니다. 그런 다음 이러한 블록을 페더레이션 계산에 통합할 수 있습니다. 고유한 페더레이션 학습 알고리즘 구축하기, 재검토 이제 ...
@tff.tf_computation def server_init(): model = model_fn() return model.trainable_variables
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
그런 다음 tff.federated_value를 사용하여 이를 페더레이션 계산에 직접 전달할 수 있습니다.
@tff.federated_computation def initialize_fn(): return tff.federated_value(server_init(), tff.SERVER)
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
next_fn 만들기 이제 클라이언트 및 서버 업데이트 코드를 사용하여 실제 알고리즘을 작성합니다. 먼저 client_update를 클라이언트 데이터세트와 서버 가중치를 받아들이고 업데이트된 클라이언트 가중치 텐서를 출력하는 tff.tf_computation으로 변환합니다. 함수를 적절하게 데코레이션하려면 해당 유형이 필요합니다. 다행히도 서버 가중치 유형은 모델에서 직접 추출할 수 있습니다.
whimsy_model = model_fn() tf_dataset_type = tff.SequenceType(whimsy_model.input_spec)
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
데이터세트 유형 서명을 살펴보겠습니다. 28 x 28 이미지(정수 레이블 포함)를 가져와 병합했다는 사실을 상기하세요.
str(tf_dataset_type)
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
위의 server_init 함수를 사용하여 모델 가중치 유형을 추출할 수도 있습니다.
model_weights_type = server_init.type_signature.result
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
형식 서명을 살펴보면 모델의 아키텍처를 볼 수 있습니다!
str(model_weights_type)
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
이제 클라이언트 업데이트를 위한 tff.tf_computation을 생성할 수 있습니다.
@tff.tf_computation(tf_dataset_type, model_weights_type) def client_update_fn(tf_dataset, server_weights): model = model_fn() client_optimizer = tf.keras.optimizers.SGD(learning_rate=0.01) return client_update(model, tf_dataset, server_weights, client_optimizer)
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
서버 업데이트의 tff.tf_computation 버전은 이미 추출한 유형을 사용하여 유사한 방식으로 정의할 수 있습니다.
@tff.tf_computation(model_weights_type) def server_update_fn(mean_client_weights): model = model_fn() return server_update(model, mean_client_weights)
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
마지막으로 이 모든 것을 통합하는 tff.federated_computation을 만들어야 합니다. 이 함수는 두 개의 페더레이션 값을 받아 들입니다. 하나는 서버 가중치(게재 위치 tff.SERVER)에 해당하고 다른 하나는 클라이언트 데이터세트(게재 위치 tff.CLIENTS)에 해당합니다. 이 두 유형은 모두 위에서 정의되었습니다! tff.FederatedType을 사용하여 적절한 배치를 제공하기만 하면 됩니다.
federated_server_type = tff.FederatedType(model_weights_type, tff.SERVER) federated_dataset_type = tff.FederatedType(tf_dataset_type, tff.CLIENTS)
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
FL 알고리즘의 4가지 요소를 기억하십니까? 서버-클라이언트 브로드캐스트 단계 로컬 클라이언트 업데이트 단계 클라이언트-서버 업로드 단계 서버 업데이트 단계 이제 위의 내용을 작성했으므로 각 부분을 한 줄의 TFF 코드로 간결하게 표현할 수 있습니다. 이 단순함 때문에 페더레이션 유형과 같은 것을 지정하기 위해 특별히 주의해야 했습니다!
@tff.federated_computation(federated_server_type, federated_dataset_type) def next_fn(server_weights, federated_dataset): # Broadcast the server weights to the clients. server_weights_at_client = tff.federated_broadcast(server_weights) # Each client computes their updated weights. client_weights = tff.federate...
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
이제 알고리즘 초기화와 알고리즘의 한 단계를 실행하기 위한 tff.federated_computation이 있습니다. 알고리즘을 완료하기 위해 이를 tff.templates.IterativeProcess에 전달합니다.
federated_algorithm = tff.templates.IterativeProcess( initialize_fn=initialize_fn, next_fn=next_fn )
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
반복 프로세스의 <code>initialize</code> 및 next 함수의 <em>유형 서명</em>을 살펴보겠습니다.
str(federated_algorithm.initialize.type_signature)
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
이는 federated_algorithm.initialize가 단일 레이어 모델(784x10 가중치 행렬 및 10개의 바이어스 단위 포함)을 반환하는 인수가 없는 함수라는 사실을 반영합니다.
str(federated_algorithm.next.type_signature)
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
여기에서 federated_algorithm.next는 서버 모델과 클라이언트 데이터를 받아들이고 업데이트된 서버 모델을 반환합니다. 알고리즘 평가 몇 라운드를 실행하고 손실이 어떻게 변하는지 살펴보겠습니다. 먼저 두 번째 튜토리얼에서 논의한 중앙 집중식 접근 방식을 사용하여 평가 기능을 정의합니다. 먼저 중앙 집중식 평가 데이터세트를 만든 다음 훈련 데이터에 사용한 것과 동일한 전처리를 적용합니다.
central_emnist_test = emnist_test.create_tf_dataset_from_all_clients() central_emnist_test = preprocess(central_emnist_test)
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
다음으로 서버 상태를 받아들이고 Keras를 사용하여 테스트 데이터세트를 평가하는 함수를 작성합니다. tf.Keras에 익숙하다면 모두 낯설지 않겠지만 set_weights의 사용에 주목하세요!
def evaluate(server_state): keras_model = create_keras_model() keras_model.compile( loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=[tf.keras.metrics.SparseCategoricalAccuracy()] ) keras_model.set_weights(server_state) keras_model.evaluate(central_emnist_test)
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
이제 알고리즘을 초기화하고 테스트세트에서 평가해 보겠습니다.
server_state = federated_algorithm.initialize() evaluate(server_state)
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
몇 라운드 동안 훈련하고 변경 사항이 있는지 살펴보겠습니다.
for round in range(15): server_state = federated_algorithm.next(server_state, federated_train_data) evaluate(server_state)
site/ko/federated/tutorials/building_your_own_federated_learning_algorithm.ipynb
tensorflow/docs-l10n
apache-2.0
Automatically, read has identified the header and the format of each column. The result is a Table object, and that brings some additional properties.
# Show the info of the data read data.info # Get the name of the columns data.colnames # Get just the values of a particular column data['obsid'] # get the first element data['obsid', 'redshift'][0]
07-Tables/reading_tables_Instructor.ipynb
OpenAstronomy/workshop_sunpy_astropy
mit
Astropy can read a variety of formats easily. The following example uses a quite
# Read the data from the source table = ascii.read("ftp://cdsarc.u-strasbg.fr/pub/cats/VII/253/snrs.dat", readme="ftp://cdsarc.u-strasbg.fr/pub/cats/VII/253/ReadMe") # See the stats of the table table.info('stats') # If we want to see the first 10 entries table[0:10] # the units are also stored, w...
07-Tables/reading_tables_Instructor.ipynb
OpenAstronomy/workshop_sunpy_astropy
mit
Properties when reading the reading of the table has many properties, let's imagine the following easy example:
weather_data = """ # Country = Finland # City = Helsinki # Longitud = 24.9375 # Latitud = 60.170833 # Week = 32 # Year = 2015 day, precip, type Mon,1.5,rain Tues,, Wed,1.1,snow Thur,2.3,rain Fri,0.2, Sat,1.1,snow Sun,5.4,snow """ # Read the table weather = ascii.read(weather_data) # Blank values are interpreted by de...
07-Tables/reading_tables_Instructor.ipynb
OpenAstronomy/workshop_sunpy_astropy
mit
When the values are not empty, then the keyword fill_values on read has to be used. Reading VOTables VOTables are an special type of tables which should be self-consistent and can be tied to a particular scheme. This mean the file will contain where the data comes from (and which query produced it) and the properties f...
from astropy.io.votable import parse_single_table # Read the example table from HELIO (hfc_ar.xml) table = parse_single_table("hfc_ar.xml") # See the fields of the table table.fields # extract one (NOAA_NUMBER) or all of the columns NOAA = table.array['NOAA_NUMBER'] # Show the data NOAA.data # See the mask NOAA.m...
07-Tables/reading_tables_Instructor.ipynb
OpenAstronomy/workshop_sunpy_astropy
mit
下面我们再编写一个方法用于快速地获得一个训练实例(即一个名字以及它所属的语言): 其中 line_index 中保存的是选择的姓氏中的字母的索引,这个需要你去实现。
import random def random_training_pair(): # 随机选择一种语言 category = random.choice(all_categories) # 从语言中随机选择一个姓氏 line = random.choice(category_lines[category]) # 我们将姓氏和语言都转化为索引 category_index = all_categories.index(category) line_index = [] # 你需要把 line 中字母的索引加入到line_index 中 # To...
jizhi-pytorch-2/02_sentiment_analysis/homework-Copy1.ipynb
liufuyang/deep_learning_tutorial
mit
编写 LSTM 模型 现在是建立 LSTM 模型的时候了。 我在模型中设置了一些空缺,你需要编写空缺处的代码。 如果遇到问题,可以参考课程中的代码讲解哦!
class LSTMNetwork(nn.Module): def __init__(self, input_size, hidden_size, output_size, n_layers=1): super(LSTMNetwork, self).__init__() self.n_layers = n_layers self.hidden_size = hidden_size # LSTM的构造如下: # 一个embedding层,将输入的任意一个单词(list)映射为一个向量(向量的维度与隐含层有关系?) self.emb...
jizhi-pytorch-2/02_sentiment_analysis/homework-Copy1.ipynb
liufuyang/deep_learning_tutorial
mit
训练网络 每次训练模型的时候,我的心里都是有点小激动的! 我同样在训练程序中预留了一些空位,你要编写空余位置的程序,训练才可以正常进行。
import time import math # 开始训练LSTM网络 n_epochs = 100000 # 构造一个LSTM网络的实例 lstm = LSTMNetwork(n_letters, 10, n_categories, 2) #定义损失函数 cost = torch.nn.NLLLoss() #定义优化器, optimizer = torch.optim.Adam(lstm.parameters(), lr = 0.001) records = [] # 用于计算训练时间的函数 def time_since(since): now = time.time() s = now - since...
jizhi-pytorch-2/02_sentiment_analysis/homework-Copy1.ipynb
liufuyang/deep_learning_tutorial
mit
通过姓氏分析语言的相似性 激动人心的时刻到了! 下面将使用10000条数据评估训练好的模型,并根据评估的结果绘制图形,从图形中我们可以发现哪些语言的姓氏是相似的! 你需要自行编写 evaluate 函数的内容。
import matplotlib.pyplot as plt # 建立一个(18 x 18)的方阵张量 # 用于保存神经网络做出的预测结果 confusion = torch.zeros(n_categories, n_categories) # 用于评估的模型的测试次数 n_confusion = 10000 # 评估用方法 传进去一个名字,给出预测结果 # 可以观察到这个方法的实现与 train 方法前半部分类似 # 其实它就是去掉反向传播的 train 方法 def evaluate(line_list): # 调用模型前应该先初始化模型的隐含层 hidden = # 别忘了将输入的list...
jizhi-pytorch-2/02_sentiment_analysis/homework-Copy1.ipynb
liufuyang/deep_learning_tutorial
mit
The Bernoulli distribution we studied earlier answers the question of which of two outcomes ($Y \in \lbrace 0,1 \rbrace$) would be selected with probability, $p$. $$ \mathbb{P}(Y) = p^Y (1-p)^{ 1-Y } $$ We also know how to solve the corresponding likelihood function for the maximum likelihood estimate of $p$ given obse...
%matplotlib inline import numpy as np from matplotlib.pylab import subplots v = 0.9 @np.vectorize def gen_y(x): if x<5: return np.random.choice([0,1],p=[v,1-v]) else: return np.random.choice([0,1],p=[1-v,v]) xi = np.sort(np.random.rand(500)*10) yi = gen_y(xi)
chapters/machine_learning/notebooks/logreg.ipynb
unpingco/Python-for-Probability-Statistics-and-Machine-Learning
mit
Programming Tip. The np.vectorize decorator used in the code above makes it easy to avoid looping in code that uses Numpy arrays by embedding the looping semantics inside of the so-decorated function. Note, however, that this does not necessarily accelerate the wrapped function. It's mainly for convenience. Figure show...
fig,ax=subplots() _=ax.plot(xi,yi,'o',color='gray',alpha=.3) _=ax.axis(ymax=1.1,ymin=-0.1) _=ax.set_xlabel(r'$X$',fontsize=22) _=ax.set_ylabel(r'$Y$',fontsize=22) from sklearn.linear_model import LogisticRegression lr = LogisticRegression() lr.fit(np.c_[xi],yi) fig,ax=subplots() xii=np.linspace(0,10,20) _=ax.plot(xii...
chapters/machine_learning/notebooks/logreg.ipynb
unpingco/Python-for-Probability-Statistics-and-Machine-Learning
mit
<!-- dom:FIGURE: [fig-machine_learning/logreg_001.png, width=500 frac=0.75] This scatterplot shows the binary $Y$ variables and the corresponding $x$ data for each category. <div id="fig:logreg_001"></div> --> <!-- begin figure --> <div id="fig:logreg_001"></div> <p>This scatterplot shows the binary $Y$ variables and ...
x0,x1=np.random.rand(2,20)*6-3 X = np.c_[x0,x1,x1*0+1] # stack as columns
chapters/machine_learning/notebooks/logreg.ipynb
unpingco/Python-for-Probability-Statistics-and-Machine-Learning
mit
Note that X has a third column of all ones. This is a trick to allow the corresponding line to be offset from the origin in the two-dimensional plane. Next, we create a linear boundary and assign the class probabilities according to proximity to the boundary.
beta = np.array([1,-1,1]) # last coordinate for affine offset prd = X.dot(beta) probs = 1/(1+np.exp(-prd/np.linalg.norm(beta))) c = (prd>0) # boolean array class labels
chapters/machine_learning/notebooks/logreg.ipynb
unpingco/Python-for-Probability-Statistics-and-Machine-Learning
mit
This establishes the training data. The next block creates the logistic regression object and fits the data.
lr = LogisticRegression() _=lr.fit(X[:,:-1],c)
chapters/machine_learning/notebooks/logreg.ipynb
unpingco/Python-for-Probability-Statistics-and-Machine-Learning
mit
Note that we have to omit the third dimension because of how Scikit-learn internally breaks down the components of the boundary. The resulting code extracts the corresponding $\boldsymbol{\beta}$ from the LogisticRegression object.
betah = np.r_[lr.coef_.flat,lr.intercept_]
chapters/machine_learning/notebooks/logreg.ipynb
unpingco/Python-for-Probability-Statistics-and-Machine-Learning
mit
Programming Tip. The Numpy np.r_ object provides a quick way to stack Numpy arrays horizontally instead of using np.hstack. The resulting boundary is shown in the left panel in Figure. The crosses and triangles represent the two classes we created above, along with the separating gray line. The logistic regression fi...
lr = LogisticRegression(C=1000)
chapters/machine_learning/notebooks/logreg.ipynb
unpingco/Python-for-Probability-Statistics-and-Machine-Learning
mit
Initialize
folder = '../sumSines/' metric = get_metric()
MES/integrals/volumeIntegral/calculations/exactSolutions.ipynb
CELMA-project/CELMA
lgpl-3.0
Define the function to volume integrate NOTE: These do not need to be fulfilled in order to get convergence z must be periodic The field $f(\rho, \theta)$ must be of class infinity in $z=0$ and $z=2\pi$ The field $f(\rho, \theta)$ must be continuous in the $\rho$ direction with $f(\rho, \theta + \pi)$ But this needs ...
# We need Lx and Ly from boututils.options import BOUTOptions myOpts = BOUTOptions(folder) Lx = eval(myOpts.geom['Lx']) Ly = eval(myOpts.geom['Ly']) # z sin function # NOTE: The function is not continuous over origo s = 2 c = pi w = pi/2 the_vars['f'] = sin(z + 2.0) + sin(2.0*z + 2.1) + sin(3.0*z)
MES/integrals/volumeIntegral/calculations/exactSolutions.ipynb
CELMA-project/CELMA
lgpl-3.0
Calculating the solution
rhoInt = integrate(the_vars['f']*x, (x, 0 ,Lx)) rhoYInt = integrate(rhoInt , (y, 0 ,Ly)) the_vars['S'] = (integrate(rhoYInt , (z, 0, 2*np.pi))).evalf()
MES/integrals/volumeIntegral/calculations/exactSolutions.ipynb
CELMA-project/CELMA
lgpl-3.0
Very simple baseline Now let's write what in NLP jargon is called a baseline, that is a method for extracting named entities that can serve as a term of comparison to evaluate the accuracy of other methods. Baseline method: - cycle through each token of the text - if the token starts with a capital letter it's a name...
"T".istitle() "t".istitle() # we need a list to store the tagged tokens tagged_tokens = [] # tokenisation is done by using the string method `split(" ")` # that splits a string upon white spaces for n, token in enumerate(de_bello_gallico_book1.split(" ")): if(token.istitle()): tagged_tokens.append((toke...
participants_notebooks/Sunoikisis - Named Entity Extraction 1b-GBclass.ipynb
mromanello/SunoikisisDC_NER
gpl-3.0
For convenience we can also wrap our baseline code into a function that we call extract_baseline. Let's define it:
def extract_baseline(input_text): """ :param input_text: the text to tag (string) :return: a list of tuples, where tuple[0] is the token and tuple[1] is the named entity tag """ # we need a list to store the tagged tokens tagged_tokens = [] # tokenisation is done by using the string method ...
participants_notebooks/Sunoikisis - Named Entity Extraction 1b-GBclass.ipynb
mromanello/SunoikisisDC_NER
gpl-3.0
We can modify slightly our function so that it prints the snippet of text where an entity is found:
def extract_baseline(input_text): """ :param input_text: the text to tag (string) :return: a list of tuples, where tuple[0] is the token and tuple[1] is the named entity tag """ # we need a list to store the tagged tokens tagged_tokens = [] # tokenisation is done by using the string method ...
participants_notebooks/Sunoikisis - Named Entity Extraction 1b-GBclass.ipynb
mromanello/SunoikisisDC_NER
gpl-3.0
The output looks slightly different from the one of our baseline function (the size of the tuples in the list varies). But we can write a function to fix this, we call it reshape_cltk_output:
def reshape_cltk_output(tagged_tokens): reshaped_output = [] for tagged_token in tagged_tokens: if(len(tagged_token)==1): continue #reshaped_output.append((tagged_token[0], "O")) else: reshaped_output.append((tagged_token[0], tagged_token[1])) return resha...
participants_notebooks/Sunoikisis - Named Entity Extraction 1b-GBclass.ipynb
mromanello/SunoikisisDC_NER
gpl-3.0
Let's have a look at the output
tagged_text_nltk[0:150]
participants_notebooks/Sunoikisis - Named Entity Extraction 1b-GBclass.ipynb
mromanello/SunoikisisDC_NER
gpl-3.0
Wrap up At this point we can "compare" the output of the three different methods we used, again by using the zip function.
list(zip(tagged_text_baseline[:50], tagged_text_cltk[:50],tagged_text_nltk[:50])) for baseline_out, cltk_out, nltk_out in zip(tagged_text_baseline[:150], tagged_text_cltk[:150], tagged_text_nltk[:150]): print("Baseline: %s\nCLTK: %s\nNLTK: %s\n"%(baseline_out, cltk_out, nltk_out))
participants_notebooks/Sunoikisis - Named Entity Extraction 1b-GBclass.ipynb
mromanello/SunoikisisDC_NER
gpl-3.0
Convolutional Autoencoders Convolutional autoencoders [3] are the adaptation of autoencoders to images (or other spacially-structured data). They are built with convolutional layers where each layer consists of a number of feature maps. Each feature map is produced by convolving a small filter with the layer's inputs, ...
from shogun import DynamicObjectArray, NeuralInputLayer, NeuralConvolutionalLayer, CMAF_RECTIFIED_LINEAR conv_layers = DynamicObjectArray() # 16x16 single channel images conv_layers.append_element(NeuralInputLayer(16,16,1)) # the first encoding layer: 5 feature maps, filters with radius 2 (5x5 filters) # and max-poo...
doc/ipython-notebooks/neuralnets/autoencoders.ipynb
lambday/shogun
bsd-3-clause
Data augmentation <table class="tfo-notebook-buttons" align="left"> <td> <a target="_blank" href="https://www.tensorflow.org/tutorials/images/data_augmentation"><img src="https://www.tensorflow.org/images/tf_logo_32px.png" />View on TensorFlow.org</a> </td> <td> <a target="_blank" href="https://colab.rese...
import matplotlib.pyplot as plt import numpy as np import tensorflow as tf import tensorflow_datasets as tfds from tensorflow.keras import layers
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Download a dataset This tutorial uses the tf_flowers dataset. For convenience, download the dataset using TensorFlow Datasets. If you would like to learn about other ways of importing data, check out the load images tutorial.
(train_ds, val_ds, test_ds), metadata = tfds.load( 'tf_flowers', split=['train[:80%]', 'train[80%:90%]', 'train[90%:]'], with_info=True, as_supervised=True, )
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
The flowers dataset has five classes.
num_classes = metadata.features['label'].num_classes print(num_classes)
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Let's retrieve an image from the dataset and use it to demonstrate data augmentation.
get_label_name = metadata.features['label'].int2str image, label = next(iter(train_ds)) _ = plt.imshow(image) _ = plt.title(get_label_name(label))
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Use Keras preprocessing layers Resizing and rescaling You can use the Keras preprocessing layers to resize your images to a consistent shape (with tf.keras.layers.Resizing), and to rescale pixel values (with tf.keras.layers.Rescaling).
IMG_SIZE = 180 resize_and_rescale = tf.keras.Sequential([ layers.Resizing(IMG_SIZE, IMG_SIZE), layers.Rescaling(1./255) ])
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Note: The rescaling layer above standardizes pixel values to the [0, 1] range. If instead you wanted it to be [-1, 1], you would write tf.keras.layers.Rescaling(1./127.5, offset=-1). You can visualize the result of applying these layers to an image.
result = resize_and_rescale(image) _ = plt.imshow(result)
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Verify that the pixels are in the [0, 1] range:
print("Min and max pixel values:", result.numpy().min(), result.numpy().max())
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Data augmentation You can use the Keras preprocessing layers for data augmentation as well, such as tf.keras.layers.RandomFlip and tf.keras.layers.RandomRotation. Let's create a few preprocessing layers and apply them repeatedly to the same image.
data_augmentation = tf.keras.Sequential([ layers.RandomFlip("horizontal_and_vertical"), layers.RandomRotation(0.2), ]) # Add the image to a batch. image = tf.cast(tf.expand_dims(image, 0), tf.float32) plt.figure(figsize=(10, 10)) for i in range(9): augmented_image = data_augmentation(image) ax = plt.subplot(3...
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
There are a variety of preprocessing layers you can use for data augmentation including tf.keras.layers.RandomContrast, tf.keras.layers.RandomCrop, tf.keras.layers.RandomZoom, and others. Two options to use the Keras preprocessing layers There are two ways you can use these preprocessing layers, with important trade-of...
model = tf.keras.Sequential([ # Add the preprocessing layers you created earlier. resize_and_rescale, data_augmentation, layers.Conv2D(16, 3, padding='same', activation='relu'), layers.MaxPooling2D(), # Rest of your model. ])
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
There are two important points to be aware of in this case: Data augmentation will run on-device, synchronously with the rest of your layers, and benefit from GPU acceleration. When you export your model using model.save, the preprocessing layers will be saved along with the rest of your model. If you later deploy ...
aug_ds = train_ds.map( lambda x, y: (resize_and_rescale(x, training=True), y))
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
With this approach, you use Dataset.map to create a dataset that yields batches of augmented images. In this case: Data augmentation will happen asynchronously on the CPU, and is non-blocking. You can overlap the training of your model on the GPU with data preprocessing, using Dataset.prefetch, shown below. In this ca...
batch_size = 32 AUTOTUNE = tf.data.AUTOTUNE def prepare(ds, shuffle=False, augment=False): # Resize and rescale all datasets. ds = ds.map(lambda x, y: (resize_and_rescale(x), y), num_parallel_calls=AUTOTUNE) if shuffle: ds = ds.shuffle(1000) # Batch all datasets. ds = ds.batch(batch_size...
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Train a model For completeness, you will now train a model using the datasets you have just prepared. The Sequential model consists of three convolution blocks (tf.keras.layers.Conv2D) with a max pooling layer (tf.keras.layers.MaxPooling2D) in each of them. There's a fully-connected layer (tf.keras.layers.Dense) with 1...
model = tf.keras.Sequential([ layers.Conv2D(16, 3, padding='same', activation='relu'), layers.MaxPooling2D(), layers.Conv2D(32, 3, padding='same', activation='relu'), layers.MaxPooling2D(), layers.Conv2D(64, 3, padding='same', activation='relu'), layers.MaxPooling2D(), layers.Flatten(), layers.Dense(128...
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Choose the tf.keras.optimizers.Adam optimizer and tf.keras.losses.SparseCategoricalCrossentropy loss function. To view training and validation accuracy for each training epoch, pass the metrics argument to Model.compile.
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Train for a few epochs:
epochs=5 history = model.fit( train_ds, validation_data=val_ds, epochs=epochs ) loss, acc = model.evaluate(test_ds) print("Accuracy", acc)
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Custom data augmentation You can also create custom data augmentation layers. This section of the tutorial shows two ways of doing so: First, you will create a tf.keras.layers.Lambda layer. This is a good way to write concise code. Next, you will write a new layer via subclassing, which gives you more control. Both l...
def random_invert_img(x, p=0.5): if tf.random.uniform([]) < p: x = (255-x) else: x return x def random_invert(factor=0.5): return layers.Lambda(lambda x: random_invert_img(x, factor)) random_invert = random_invert() plt.figure(figsize=(10, 10)) for i in range(9): augmented_image = random_invert(im...
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Next, implement a custom layer by subclassing:
class RandomInvert(layers.Layer): def __init__(self, factor=0.5, **kwargs): super().__init__(**kwargs) self.factor = factor def call(self, x): return random_invert_img(x) _ = plt.imshow(RandomInvert()(image)[0])
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Both of these layers can be used as described in options 1 and 2 above. Using tf.image The above Keras preprocessing utilities are convenient. But, for finer control, you can write your own data augmentation pipelines or layers using tf.data and tf.image. (You may also want to check out TensorFlow Addons Image: Operati...
(train_ds, val_ds, test_ds), metadata = tfds.load( 'tf_flowers', split=['train[:80%]', 'train[80%:90%]', 'train[90%:]'], with_info=True, as_supervised=True, )
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Retrieve an image to work with:
image, label = next(iter(train_ds)) _ = plt.imshow(image) _ = plt.title(get_label_name(label))
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Let's use the following function to visualize and compare the original and augmented images side-by-side:
def visualize(original, augmented): fig = plt.figure() plt.subplot(1,2,1) plt.title('Original image') plt.imshow(original) plt.subplot(1,2,2) plt.title('Augmented image') plt.imshow(augmented)
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Data augmentation Flip an image Flip an image either vertically or horizontally with tf.image.flip_left_right:
flipped = tf.image.flip_left_right(image) visualize(image, flipped)
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Grayscale an image You can grayscale an image with tf.image.rgb_to_grayscale:
grayscaled = tf.image.rgb_to_grayscale(image) visualize(image, tf.squeeze(grayscaled)) _ = plt.colorbar()
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Saturate an image Saturate an image with tf.image.adjust_saturation by providing a saturation factor:
saturated = tf.image.adjust_saturation(image, 3) visualize(image, saturated)
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Change image brightness Change the brightness of image with tf.image.adjust_brightness by providing a brightness factor:
bright = tf.image.adjust_brightness(image, 0.4) visualize(image, bright)
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Center crop an image Crop the image from center up to the image part you desire using tf.image.central_crop:
cropped = tf.image.central_crop(image, central_fraction=0.5) visualize(image, cropped)
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Rotate an image Rotate an image by 90 degrees with tf.image.rot90:
rotated = tf.image.rot90(image) visualize(image, rotated)
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Random transformations Warning: There are two sets of random image operations: tf.image.random* and tf.image.stateless_random*. Using tf.image.random* operations is strongly discouraged as they use the old RNGs from TF 1.x. Instead, please use the random image operations introduced in this tutorial. For more informatio...
for i in range(3): seed = (i, 0) # tuple of size (2,) stateless_random_brightness = tf.image.stateless_random_brightness( image, max_delta=0.95, seed=seed) visualize(image, stateless_random_brightness)
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Randomly change image contrast Randomly change the contrast of image using tf.image.stateless_random_contrast by providing a contrast range and seed. The contrast range is chosen randomly in the interval [lower, upper] and is associated with the given seed.
for i in range(3): seed = (i, 0) # tuple of size (2,) stateless_random_contrast = tf.image.stateless_random_contrast( image, lower=0.1, upper=0.9, seed=seed) visualize(image, stateless_random_contrast)
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Randomly crop an image Randomly crop image using tf.image.stateless_random_crop by providing target size and seed. The portion that gets cropped out of image is at a randomly chosen offset and is associated with the given seed.
for i in range(3): seed = (i, 0) # tuple of size (2,) stateless_random_crop = tf.image.stateless_random_crop( image, size=[210, 300, 3], seed=seed) visualize(image, stateless_random_crop)
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Apply augmentation to a dataset Let's first download the image dataset again in case they are modified in the previous sections.
(train_datasets, val_ds, test_ds), metadata = tfds.load( 'tf_flowers', split=['train[:80%]', 'train[80%:90%]', 'train[90%:]'], with_info=True, as_supervised=True, )
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Next, define a utility function for resizing and rescaling the images. This function will be used in unifying the size and scale of images in the dataset:
def resize_and_rescale(image, label): image = tf.cast(image, tf.float32) image = tf.image.resize(image, [IMG_SIZE, IMG_SIZE]) image = (image / 255.0) return image, label
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Let's also define the augment function that can apply the random transformations to the images. This function will be used on the dataset in the next step.
def augment(image_label, seed): image, label = image_label image, label = resize_and_rescale(image, label) image = tf.image.resize_with_crop_or_pad(image, IMG_SIZE + 6, IMG_SIZE + 6) # Make a new seed. new_seed = tf.random.experimental.stateless_split(seed, num=1)[0, :] # Random crop back to the original si...
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Option 1: Using tf.data.experimental.Counter Create a tf.data.experimental.Counter object (let's call it counter) and Dataset.zip the dataset with (counter, counter). This will ensure that each image in the dataset gets associated with a unique value (of shape (2,)) based on counter which later can get passed into the ...
# Create a `Counter` object and `Dataset.zip` it together with the training set. counter = tf.data.experimental.Counter() train_ds = tf.data.Dataset.zip((train_datasets, (counter, counter)))
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Map the augment function to the training dataset:
train_ds = ( train_ds .shuffle(1000) .map(augment, num_parallel_calls=AUTOTUNE) .batch(batch_size) .prefetch(AUTOTUNE) ) val_ds = ( val_ds .map(resize_and_rescale, num_parallel_calls=AUTOTUNE) .batch(batch_size) .prefetch(AUTOTUNE) ) test_ds = ( test_ds .map(resize_and_resc...
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Option 2: Using tf.random.Generator Create a tf.random.Generator object with an initial seed value. Calling the make_seeds function on the same generator object always returns a new, unique seed value. Define a wrapper function that: 1) calls the make_seeds function; and 2) passes the newly generated seed value into t...
# Create a generator. rng = tf.random.Generator.from_seed(123, alg='philox') # Create a wrapper function for updating seeds. def f(x, y): seed = rng.make_seeds(2)[0] image, label = augment((x, y), seed) return image, label
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Map the wrapper function f to the training dataset, and the resize_and_rescale function—to the validation and test sets:
train_ds = ( train_datasets .shuffle(1000) .map(f, num_parallel_calls=AUTOTUNE) .batch(batch_size) .prefetch(AUTOTUNE) ) val_ds = ( val_ds .map(resize_and_rescale, num_parallel_calls=AUTOTUNE) .batch(batch_size) .prefetch(AUTOTUNE) ) test_ds = ( test_ds .map(resize_and_resc...
site/en/tutorials/images/data_augmentation.ipynb
tensorflow/docs
apache-2.0
Fitting multiple linear regression to the training set
from sklearn.linear_model import LinearRegression regressor = LinearRegression() regressor.fit(X_train,Y_train) # Predicting the test set results Y_pred = regressor.predict(X_test)
Part-2-Regression/P2-Multiple_Linear_Regression.ipynb
snbhanja/A-Z-Machine-Learning---Udemy-
mit
Building the optimal model using Backward elimination Previously to build the multiple regression model we used all the independent variable. Out of these some independent variable are higly statistically significant and some are not
import statsmodels.formula.api as sm # The multiple linear regression equation is # y = b0 + b1.X^1 + b2.x^2 + ... + bn.x^n # The stats model requires that b0 is actually b0.x^0 # So, X^0 here is column vector of 1's # we will add this vector of 1's to X X = np.append(arr = np.ones((50, 1)).astype(int), values = X, ax...
Part-2-Regression/P2-Multiple_Linear_Regression.ipynb
snbhanja/A-Z-Machine-Learning---Udemy-
mit
Load and Prepare the Data Check out the first example notebook to learn more about the data and format.
from tsfresh.examples.robot_execution_failures import download_robot_execution_failures download_robot_execution_failures() df_ts, y = load_robot_execution_failures()
notebooks/examples/02 sklearn Pipeline.ipynb
blue-yonder/tsfresh
mit
We want to use the extracted features to predict for each of the robot executions, if it was a failure or not. Therefore our basic "entity" is a single robot execution given by a distinct id. A dataframe with these identifiers as index needs to be prepared for the pipeline.
X = pd.DataFrame(index=y.index) # Split data into train and test set X_train, X_test, y_train, y_test = train_test_split(X, y)
notebooks/examples/02 sklearn Pipeline.ipynb
blue-yonder/tsfresh
mit
Build the pipeline We build a sklearn pipeline that consists of a feature extraction step (RelevantFeatureAugmenter) with a subsequent RandomForestClassifier. The RelevantFeatureAugmenter takes roughly the same arguments as extract_features and select_features do.
ppl = Pipeline([ ('augmenter', RelevantFeatureAugmenter(column_id='id', column_sort='time')), ('classifier', RandomForestClassifier()) ])
notebooks/examples/02 sklearn Pipeline.ipynb
blue-yonder/tsfresh
mit
<div class="alert alert-warning"> Here comes the tricky part! The input to the pipeline will be our dataframe `X`, which one row per identifier. It is currently empty. But which time series data should the `RelevantFeatureAugmenter` to actually extract the features from? We need to pass the time series data (stored ...
ppl.set_params(augmenter__timeseries_container=df_ts);
notebooks/examples/02 sklearn Pipeline.ipynb
blue-yonder/tsfresh
mit
We are now ready to fit the pipeline
ppl.fit(X_train, y_train)
notebooks/examples/02 sklearn Pipeline.ipynb
blue-yonder/tsfresh
mit
The augmenter has used the input time series data to extract time series features for each of the identifiers in the X_train and selected only the relevant ones using the passed y_train as target. These features have been added to X_train as new columns. The classifier can now use these features during trainings. Predi...
y_pred = ppl.predict(X_test)
notebooks/examples/02 sklearn Pipeline.ipynb
blue-yonder/tsfresh
mit
So, finally we inspect the performance:
print(classification_report(y_test, y_pred))
notebooks/examples/02 sklearn Pipeline.ipynb
blue-yonder/tsfresh
mit
You can also find out, which columns the augmenter has selected
ppl.named_steps["augmenter"].feature_selector.relevant_features
notebooks/examples/02 sklearn Pipeline.ipynb
blue-yonder/tsfresh
mit
<div class="alert alert-info"> In this example we passed in an empty (except the index) `X_train` or `X_test` into the pipeline. However, you can also fill the input with other features you have (e.g. features extracted from the metadata) or even use other pipeline components before. </div> Separating the time serie...
df_ts_train = df_ts[df_ts["id"].isin(y_train.index)] df_ts_test = df_ts[df_ts["id"].isin(y_test.index)] ppl.set_params(augmenter__timeseries_container=df_ts_train); ppl.fit(X_train, y_train); import pickle with open("pipeline.pkl", "wb") as f: pickle.dump(ppl, f)
notebooks/examples/02 sklearn Pipeline.ipynb
blue-yonder/tsfresh
mit
Later: load the fitted model and do predictions on new, unseen data
import pickle with open("pipeline.pkl", "rb") as f: ppk = pickle.load(f) ppl.set_params(augmenter__timeseries_container=df_ts_test); y_pred = ppl.predict(X_test) print(classification_report(y_test, y_pred))
notebooks/examples/02 sklearn Pipeline.ipynb
blue-yonder/tsfresh
mit
Get Classifier Results
plots_dir = pathlib.Path("./plots/") if not plots_dir.is_dir(): plots_dir.mkdir() features = combined.loc[:,["g_minus_r", "r_minus_i", "i_minus_z", "z_minus_y", "icmodel_mag", "photoz_best", "photoz_risk_best" # The risk of photoz_be...
dwarfz/catalog_only_classifier/just plotting RF.ipynb
egentry/dwarf_photo-z
mit
Exercise 1: Define variables str1 and str2 containing strings "The greatest glory in living lies not in never falling, " and "but in rising every time we fall", respectively. Concatenate both strings to compose a sentence, transform it to capital letters and print the result.
# Write your solution here # <SOL> # </SOL>
P1.Python_intro/P1_Starting_with_Python_student.ipynb
ML4DS/ML4all
mit
Exercise 2: Define variable odds containing all odd numbers betewen 1 and 9. Also, define variable prime containing all prime numbers between 1 and 9. Now, define variable odd_or_prime as the concatenation of odd and prime. Some numbers are repeated. User method remove to supress elements appearing twice. Print the res...
# Write your solution here # <SOL> # </SOL>
P1.Python_intro/P1_Starting_with_Python_student.ipynb
ML4DS/ML4all
mit
Exercise 3: After the exam review, you teacher has updated your mark in Statistics to 7.0. Also, the scores of "communication theory" have been just published, and you've got 10.0. How would you modify your dictionary with the new marks?
# Write your solution here # <SOL> # </SOL> print(my_marks)
P1.Python_intro/P1_Starting_with_Python_student.ipynb
ML4DS/ML4all
mit
Exercise 4: Create a list powers2 of all powers of 2 between $2^0$ (2**0) and $2^{16}$ (2**16). Loop over the list to create a new list y with all powers of 2 that are greater than 100 and lower than 1000
# Write your solution here # <SOL> # </SOL> print(y)
P1.Python_intro/P1_Starting_with_Python_student.ipynb
ML4DS/ML4all
mit
Exercise 5: Now try it yourself! Write a function called my_factorial that computes the fatorial of a given number. If you're feeling brave today, you can try writing it recursively since Python obviously supports recursion. Remember that the factorial of 0 is 1 and the factorial of a negative number is undefined.
# Write your solution here # <SOL> # </SOL> # Let's try it out! from math import factorial x = int(input('Please enter an integer: ')) if my_factorial(x) == factorial(x): print(x, '! = ', my_factorial(x)) print('It works!') else: print("Something's not working..." )
P1.Python_intro/P1_Starting_with_Python_student.ipynb
ML4DS/ML4all
mit