# Notebook from Anonymous7code/MadeWithML
Path: notebooks/02_Python.ipynb

 Made With ML

Applied ML · MLOps · Production

Join 20K+ developers in learning how to responsibly deliver value with ML.

🔥  Among the top ML repositories on GitHub

_____no_output_____# Python

In this lessons, we'll learn about the basics of Python programmming. _____no_output_____

_____no_output_____#  Variables_____no_output_____Variables are containers for holding data and they're defined by a name and value.

_____no_output_____

# Integer variable
x = 5
print (x)
print (type(x))
5
<class 'int'>

We can change the value of a variable by simply assigning a new value to it._____no_output_____

# String variable
x = "hello"
print (x)
print (type(x))hello
<class 'str'>

There are many different types of variables: integers, floats, strings, boolean etc._____no_output_____

# int variable
x = 5
print (x, type(x))5 <class 'int'>
# float variable
x = 5.0
print (x, type(x))5.0 <class 'float'>
# text variable
x = "5"
print (x, type(x))5 <class 'str'>
# boolean variable
x = True
print (x, type(x))True <class 'bool'>

We can also do operations with variables._____no_output_____

# Variables can be used with each other
a = 1
b = 2
c = a + b
print (c)3

We should always know what types of variables we're dealing with so we can do the right operations with them. Here's a common mistake that can happen if we're using the wrong variable type._____no_output_____

# int variables
a = 5
b = 3
print (a + b)8
# string variables
a = "5"
b = "3"
print (a + b)53

#  Lists_____no_output_____Lists are an ordered, mutable (changeable) collection of values that are *comma separated* and enclosed by *square brackets*. A list can be comprised of many different types of variables (below is a list with an integer, string and a float)._____no_output_____

# Creating a list
x = [3, "hello", 1.2]
print (x)[3, 'hello', 1.2]
# Length of a list
len(x)_____no_output_____

You can add to a list by using the **append** function._____no_output_____

# Adding to a list
x.append(7)
print (x)
print (len(x))[3, 'hello', 1.2, 7]
4
# Replacing items in a list
x[1] = "bye"
print (x)[3, 'bye', 1.2, 7]
# Operations
y = [2.4, "world"]
z = x + y
print (z)[3, 'bye', 1.2, 7, 2.4, 'world']

# Indexing and Slicing_____no_output_____Indexing and slicing from lists allow us to retrieve specific values within lists. Note that indices can be positive (starting from 0) or negative (-1 and lower, where -1 is the last item in the list).

_____no_output_____

# Indexing
x = [3, "hello", 1.2]
print ("x[0]: ", x[0])
print ("x[1]: ", x[1])
print ("x[-1]: ", x[-1]) # the last item
print ("x[-2]: ", x[-2]) # the second to last itemx[0]:  3
x[1]:  hello
x[-1]:  1.2
x[-2]:  hello
# Slicing
print ("x[:]: ", x[:]) # all indices
print ("x[1:]: ", x[1:]) # index 1 to the end of the list
print ("x[1:2]: ", x[1:2]) # index 1 to index 2 (not including index 2)
print ("x[:-1]: ", x[:-1]) # index 0 to last index (not including last index)x[:]:  [3, 'hello', 1.2]
x[1:]:  ['hello', 1.2]
x[1:2]:  ['hello']
x[:-1]:  [3, 'hello']

# Tuples_____no_output_____Tuples are collections that are ordered and immutable (unchangeable). You will use these to store values that will never be changed._____no_output_____

# Creating a tuple
x = (3.0, "hello") # tuples start and end with ()
print (x)(3.0, 'hello')
# Adding values to a tuple
x = x + (5.6, 4)
print (x)(3.0, 'hello', 5.6, 4)
# Try to change (it won't work and you'll get an error)
# x[0] = 1.2_____no_output_____

# Dictionaries_____no_output_____Dictionaries are an unordered, mutable and indexed collection of key-value pairs. You can retrieve values based on the key and a dictionary cannot have two of the same keys.

_____no_output_____

# Creating a dictionary
person = {'name': 'Goku',
          'eye_color': 'brown'}
print (person)
print (person['name'])
print (person['eye_color']){'name': 'Goku', 'eye_color': 'brown'}
Goku
brown
# Changing the value for a key
person['eye_color'] = 'green'
print (person){'name': 'Goku', 'eye_color': 'green'}
# Adding new key-value pairs
person['age'] = 24
print (person){'name': 'Goku', 'eye_color': 'green', 'age': 24}
# Length of a dictionary
print (len(person))3

# If statements_____no_output_____We can use `if` statements to conditionally do something. The conditions are defined by the words `if`, `elif` (which stands for else if) and `else`. We can have as many `elif` statements as we want. The indented code below each condition is the code that will execute if the condition is `True`._____no_output_____

# If statement
x = 4
if x < 1:
    score = 'low'
elif x <= 4: # elif = else if
    score = 'medium'
else:
    score = 'high'
print (score)medium
# If statement with a boolean
x = True
if x:
    print ("it worked")it worked

# Loops_____no_output_____### For Loops_____no_output_____A `for` loop can iterate over a collection of values (lists, tuples, dictionaries, etc.) The indented code is executed for each item in the collection of values._____no_output_____

# For loop
veggies = ["carrots", "broccoli", "beans"]
for veggie in veggies:
    print (veggie)carrots
broccoli
beans

When the loop encounters the `break` command, the loop will terminate immediately. If there were more items in the list, they will not be processed._____no_output_____

# `break` from a for loop
veggies = ["carrots", "broccoli", "beans"]
for veggie in veggies:
    if veggie == "broccoli":
        break
    print (veggie)carrots

When the loop encounters the `continue` command, the loop will skip all other operations for that item in the list only. If there were more items in the list, the loop will continue normally._____no_output_____

# `continue` to the next iteration
veggies = ["carrots", "broccoli", "beans"]
for veggie in veggies:
    if veggie == "broccoli":
        continue
    print (veggie)carrots
beans

### While Loops_____no_output_____A `while` loop can perform repeatedly as long as a condition is `True`. We can use `continue` and `break` commands in `while` loops as well._____no_output_____

# While loop
x = 3
while x > 0:
    x -= 1 # same as x = x - 1
    print (x)2
1
0

# Functions_____no_output_____Functions are a way to modularize reusable pieces of code. They're defined by the keyword `def` which stands for definition and they can have the following components.

_____no_output_____

# Define the function
def add_two(x):
    """Increase x by 2.""" # explains what this function will do
    x += 2
    return x_____no_output_____

Here are the components that may be required when we want to use the function. we need to ensure that the function name and the input parameters match with how we defined the function above.

_____no_output_____

# Use the function
score = 0
new_score = add_two(x=score)
print (new_score)2

A function can have as many input parameters and outputs as we want._____no_output_____

# Function with multiple inputs
def join_name(first_name, last_name):
    """Combine first name and last name."""
    joined_name = first_name + " " + last_name
    return joined_name_____no_output_____# Use the function
first_name = "Goku"
last_name = "Mohandas"
joined_name = join_name(first_name=first_name,
                        last_name=last_name)
print (joined_name)Goku Mohandas

> It's good practice to always use keyword argument when using a function so that it's very clear what input variable belongs to what function input parameter. On a related note, you will often see the terms `*args` and `**kwargs` which stand for arguments and keyword arguments. You can extract them when they are passed into a function. The significance of the `*` is that any number of arguments and keyword arguments can be passed into the function._____no_output_____

def f(*args, **kwargs):
    x = args[0]
    y = kwargs.get('y')
    print (f"x: {x}, y: {y}")_____no_output_____f(5, y=2)x: 5, y: 2

# Classes_____no_output_____Classes are object constructors and are a fundamental component of object oriented programming in Python. They are composed of a set of functions that define the class and it's operations._____no_output_____### `__init__` function_____no_output_____The `__init__` function is used when an instance of the class is initialized. _____no_output_____

# Creating the class
class Pet(object):
    """Class object for a pet."""

    def __init__(self, species, name):
        """Initialize a Pet."""
        self.species = species
        self.name = name_____no_output_____# Creating an instance of a class
my_dog = Pet(species="dog",
             name="Scooby")
print (my_dog)
print (my_dog.name)<__main__.Pet object at 0x7f7cab4cbac8>
Scooby

### `__str__` function_____no_output_____The `print (my_dog)` command printed something not so relevant to us. Let's fix that with the `__str__` function._____no_output_____

# Creating the class
class Pet(object):
    """Class object for a pet."""

    def __init__(self, species, name):
        """Initialize a Pet."""
        self.species = species
        self.name = name

    def __str__(self):
        """Output when printing an instance of a Pet."""
        return f"{self.species} named {self.name}"_____no_output_____# Creating an instance of a class
my_dog = Pet(species="dog",
             name="Scooby")
print (my_dog)
print (my_dog.name)dog named Scooby
Scooby

> Classes can be customized with **magic** functions like `__init__` and `__str__`, to enable powerful operations. We'll be exploring additional built-in functions in subsequent notebooks (like `__len__`, `__iter__` and `__getitem__`, etc.) but if you're curious, here is a [tutorial](https://rszalski.github.io/magicmethods/) on more magic methods._____no_output_____### Object methods_____no_output_____

# Creating the class
class Pet(object):
    """Class object for a pet."""

    def __init__(self, species, name):
        """Initialize a Pet."""
        self.species = species
        self.name = name

    def __str__(self):
        """Output when printing an instance of a Pet."""
        return f"{self.species} named {self.name}"

    def change_name(self, new_name):
        """Change the name of your Pet."""
        self.name = new_name_____no_output_____# Creating an instance of a class
my_dog = Pet(species="dog", name="Scooby")
print (my_dog)
print (my_dog.name)dog named Scooby
Scooby
# Using a class's function
my_dog.change_name(new_name="Scrappy")
print (my_dog)
print (my_dog.name)dog named Scrappy
Scrappy

### Inheritance_____no_output_____Inheritance allows us to inherit all the properties and methods from another class (the parent). Notice how we inherited the initialized variables from the parent `Pet` class like species and name. We also inherited the `change_name` function. But for the `__str__` function, we define our own version to overwrite the `Pet` `__str__` function. We can similarly overwrite any object functions as well._____no_output_____

class Dog(Pet):
    def __init__(self, name, breed):
        super().__init__(species="dog", name=name)
        self.breed = breed

    def __str__(self):
        return f"{self.breed} named {self.name}"_____no_output_____scooby = Dog(breed="Great Dane", name="Scooby")
print (scooby)Great Dane named Scooby
scooby.change_name('Scooby Doo')
print (scooby)Great Dane named Scooby Doo

### Methods_____no_output_____There are two important decorator methods to know about when it comes to classes: `@classmethod` and `@staticmethod`. We'll learn about decorators in the next section below but these specific methods pertain to classes so we'll cover them here._____no_output_____

class Dog(Pet):
    def __init__(self, name, breed):
        super().__init__(species="dog", name=name)
        self.breed = breed

    def __str__(self):
        return f"{self.breed} named {self.name}"

    @classmethod
    def from_dict(cls, d):
        return cls(name=d["name"], breed=d["breed"])

    @staticmethod
    def is_cute(breed):
        return True  # all animaals are cute!_____no_output_____

A `@classmethod` allows us to create class instances by passing in the uninstantiated class itself (`cls`). This is a great way to create (or load) classes from objects (ie. dictionaries). _____no_output_____

# Create instance
d = {"name": "Cassie", "breed": "Border Collie"}
cassie = Dog.from_dict(d=d)
print(cassie)Border Collie named Cassie

A `@staticmethod` can be called from an uninstantiated class object so we can do things like this:_____no_output_____

# Static method
Dog.is_cute(breed="Border Collie")_____no_output_____

# Decorators_____no_output_____Recall that functions allow us to modularize code and reuse them. However, we'll often want to add some functionality before or after the main function executes and we may want to do this for many different functions. Instead of adding more code to the original function, we can use decorators!

*    **decorators**: augment a function with pre/post-processing. Decorators wrap around the main function and allow us to operate on the inputs and or outputs._____no_output_____Suppose we have a function called `operations` which increments the input value x by 1._____no_output_____

def operations(x):
    """Basic operations."""
    x += 1
    return x_____no_output_____operations(x=1)_____no_output_____

Now let's say we want to increment our input x by 1 before and after the `operations` function executes and, to illustrate this example, let's say the increments have to be separate steps. Here's how we would do it by changing the original code:_____no_output_____

def operations(x):
    """Basic operations."""
    x += 1
    x += 1
    x += 1
    return x_____no_output_____operations(x=1)_____no_output_____

We were able to achieve what we want but we now increased the size of our `operations` function and if we want to do the same incrementing for any other function, we have to add the same code to all of those as well ... not very efficient. To solve this, let's create a decorator called `add` which increments `x` by 1 before and after the main function `f` executes._____no_output_____### Creating a decorator function_____no_output_____The decorator function accepts a function `f` which is the function we wish to wrap around, in our case, it's `operations()`. The output of the decorator is its `wrapper` function which receives the arguments and keyword arguments passed to function `f`.

Inside the `wrapper` function, we can:
1. extract the input parameters passed to function `f`.
2. make any changes we want to the function inputs.
3. function `f` is executed
4. make any changes to the function outputs
5. `wrapper` function returns some value(s), which is what the decorator returns as well since it returns `wrapper`._____no_output_____

# Decorator
def add(f):
    def wrapper(*args, **kwargs):
        """Wrapper function for @add."""
        x = kwargs.pop('x') # .get() if not altering x
        x += 1 # executes before function f
        x = f(*args, **kwargs, x=x)
        x += 1 # executes after function f
        return x
    return wrapper_____no_output_____

We can use this decorator by simply adding it to the top of our main function preceded by the `@` symbol. _____no_output_____

@add
def operations(x):
    """Basic operations."""
    x += 1
    return x_____no_output_____operations(x=1)_____no_output_____

Suppose we wanted to debug and see what function actually executed with `operations()`. _____no_output_____

operations.__name__, operations.__doc_______no_output_____

The function name and docstring are not what we're looking for but it appears this way because the `wrapper` function is what was executed. In order to fix this, Python offers `functools.wraps` which carries the main function's metadata. _____no_output_____

from functools import wraps_____no_output_____# Decorator
def add(f):
    @wraps(f)
    def wrap(*args, **kwargs):
        """Wrapper function for @add."""
        x = kwargs.pop('x')
        x += 1
        x = f(*args, **kwargs, x=x)
        x += 1
        return x
    return wrap_____no_output_____@add
def operations(x):
    """Basic operations."""
    x += 1
    return x_____no_output_____operations.__name__, operations.__doc_______no_output_____

Awesome! We were able to decorate our main function `operation()` to achieve the customization we wanted without actually altering the function. We can reuse our decorator for other functions that may need the same customization!

> This was a dummy example to show how decorators work but we'll be using them heavily during our [MLOps](https://madewithml.com/#mlops) lessons. A simple scenario would be using decorators to create uniform JSON responses from each API endpoint without including the bulky code in each endpoint._____no_output_____# Callbacks_____no_output_____Decorators allow for customized operations before and after the main function's execution but what about in between? Suppose we want to conditionally/situationally do some operations. Instead of writing a whole bunch of if-statements and make our functions bulky, we can use callbacks!

*   **callbacks**: conditional/situational processing within the function._____no_output_____Our callbacks will be classes that have functions with key names that will execute at various periods during the main function's execution. The function names are up to us but we need to invoke the same callback functions within our main function._____no_output_____

# Callback
class x_tracker(object):
    def __init__(self, x):
        self.history = []
    def at_start(self, x):
        self.history.append(x)
    def at_end(self, x):
        self.history.append(x)_____no_output_____

We can pass in as many callbacks as we want and because they have appropriately named functions, they will be invoked at the appropriate times. _____no_output_____

def operations(x, callbacks=[]):
    """Basic operations."""
    for callback in callbacks:
        callback.at_start(x)
    x += 1
    for callback in callbacks:
        callback.at_end(x)
    return x_____no_output_____x = 1
tracker = x_tracker(x=x)
operations(x=x, callbacks=[tracker])_____no_output_____tracker.history_____no_output_____

# Putting it all together_____no_output_____decorators + callbacks = powerful customization *before*, *during* and *after* the main function’s execution without increasing its complexity. We will be using this duo to create powerful ML training scripts that are highly customizable in future lessons._____no_output_____

from functools import wraps_____no_output_____# Decorator
def add(f):
    @wraps(f)
    def wrap(*args, **kwargs):
        """Wrapper function for @add."""
        x = kwargs.pop('x') # .get() if not altering x
        x += 1 # executes before function f
        x = f(*args, **kwargs, x=x)
        # can do things post function f as well
        return x
    return wrap_____no_output_____# Callback
class x_tracker(object):
    def __init__(self, x):
        self.history = [x]
    def at_start(self, x):
        self.history.append(x)
    def at_end(self, x):
        self.history.append(x)_____no_output_____# Main function
@add
def operations(x, callbacks=[]):
    """Basic operations."""
    for callback in callbacks:
        callback.at_start(x)
    x += 1
    for callback in callbacks:
        callback.at_end(x)
    return x_____no_output_____x = 1
tracker = x_tracker(x=x)
operations(x=x, callbacks=[tracker])_____no_output_____tracker.history_____no_output_____


# Notebook from jnaiman/csci-p-14110_su2019
Path: lesson09/.ipynb_checkpoints/_Part1_programming_planetsWithSurfaces_lesson09-checkpoint.ipynb

# Day 9, Part 1 - stills with planet surfaces_____no_output_____

# import our usual stuffs
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline_____no_output_____

Read in our 2 planet kepler system:_____no_output_____

from hermite_library import read_hermite_solution_from_file

# as a test:
t_h, E_h, r_h, v_h = read_hermite_solution_from_file('myPlanetSystem_kepler101_solution1.txt')_____no_output_____# we also want to grab a unit conversion for later:
from hermite_library import AUinCM_____no_output_____

To load and save things correctly, we need to define a few file structures:

First:

This will point to the directory where generic.obj, generic.mtl, make3dplanets.py, etc is stored.

This is the unzipped "genericPlanetFiles.zip" you can download from the website & unpack.

Probably the easiest thing to do is do it in your local, current directory.

**NOTE: SHOW THIS IN CLASS**_____no_output_____

make3dplanets_libdir = '/Users/jillnaiman1/Downloads/planetFiles/'_____no_output_____

Now we'll name your planet system.  This is the name of the directory where the .obj and .mtl and texture files will be stored._____no_output_____

SystemName = 'MyPlanetSystemSingle' _____no_output_____

Now list the full path where the SystemName directory where all the generated obj and mtl files will be stored.

If this directory doesn't exist, make sure to mkdir it with something like:
```!mkdir /Users/jillnaiman1/Downloads/outPlanets```_____no_output_____

output_planet_dir = '/Users/jillnaiman1/Downloads/outPlanets/'_____no_output_____

Now we need to make some decisions about what our planets will look like.

If we recall the order of our planets will be:

```planet 1, planet 2, star```

Let's remind ourselves of their masses:_____no_output_____

# make sure convert_kepler_data.py is in your directory as well
from convert_kepler_data import read_kepler_data

# make sure this datafile is in your local directory
kepler_data = read_kepler_data('kepler101data.txt')_____no_output_____# masses:
kepler_data['pMass']

# so, 0.16 and 0.01 Jupiter masses_____no_output_____kepler_data['sMass']_____no_output_____

There aren't any planets in our solar system (so the textures that we have) with those exact masses but we can estimate that we have a Saturn-ish ($0.3 M_J$) and Neptune-ish ($0.054 M_J$) sized planets and use their textures.

Similarly, while this star's mass is a little larger than our Sun, let's assume its about a solar mass and use this mass.  So, in summary:

The masses will be:

```0.16 Jupiter, 0.01 Jupiter, Solar mass```

So we can use the following textures as a first approximation:

```Saturn, Neptune, Sun```

with comprable radii._____no_output_____

texture_files = ['saturn_1024.jpg', 'neptunemap_1000.jpg', 'sun_texture1.jpg']_____no_output_____#               Saturn,    Neptune        Sun
PlanetRadius =  [5.8232e9,  2.4622e9, 69.551e9] # cm_____no_output_____

You have the option to specify colors instead of textures, but we won't be using this option, so you can just put in any place holder colors you want!_____no_output_____

PlanetColors = [  (1.0, 0, 0),  (1.0, 0.0, 0.0),    (1.0, 0.0, 0.0)]_____no_output_____

Now let's grab the planet solving library from what we downloaded from the course website:_____no_output_____

from sys import path
# point to where the library is stored
path.append(make3dplanets_libdir)
# now import make3dplanets
from make3dplanets import make3dplanets_____no_output_____

Just in case things are stored in different places we'll label these explicitly, but you shouldn't have to change this though._____no_output_____

generic_dir = make3dplanets_libdir
textures_dir = make3dplanets_libdir + 'textureMaps/'_____no_output_____

We also need to pick a timestep to make our static model out of:_____no_output_____

Nplot = 100_____no_output_____

Now let's make the planet files!  Note the weird switching of axis in the for loop - this is to make the planet textures "upright" w/rt z-axis._____no_output_____

PlanetLocation = np.zeros( [len(PlanetRadius), 3] )
for p in range(0,len(PlanetRadius)):
    PlanetLocation[p,2] = r_h[p,0,Nplot]*AUinCM # units
    PlanetLocation[p,0] = r_h[p,1,Nplot]*AUinCM
    PlanetLocation[p,1] = r_h[p,2,Nplot]*AUinCM

fname = make3dplanets(SystemName, PlanetLocation, PlanetRadius,
                      output_planet_dir, generic_dir, textures_dir=textures_dir,
                      texture_file=texture_files,  DistanceUnits=AUinCM)

fname # see what this is_____no_output_____

Now, let's open this up in MeshLab and see what we can see! **do it**_____no_output_____It is very likely you will see absolutely nothing in your systems!  Why is this? Well this is because if we do everything *to scale* the space between the planets is so much larger than the planets & stars themselves that we can't actually see them.  So we are going to make an aethetic choice here and scale up the radii by a large factor.

This is something we would have to add somewhere to the documentation of our viz in order not to confuse our viewers!

If you need to scale up your radii, you can try:_____no_output_____

#               Saturn,    Neptune        Sun
PlanetRadius =  [5.8232e9,  2.4622e9, 69.551e9] # cm
for i in range(len(PlanetRadius)): PlanetRadius[i] *= 5_____no_output_____PlanetLocation = np.zeros( [len(PlanetRadius), 3] )
for p in range(0,len(PlanetRadius)):
    PlanetLocation[p,2] = r_h[p,0,Nplot]*AUinCM # units
    PlanetLocation[p,0] = r_h[p,1,Nplot]*AUinCM
    PlanetLocation[p,1] = r_h[p,2,Nplot]*AUinCM

fname = make3dplanets(SystemName, PlanetLocation, PlanetRadius,
                      output_planet_dir, generic_dir, textures_dir=textures_dir,
                      texture_file=texture_files,  DistanceUnits=AUinCM)_____no_output_____

Now if we open this up in MeshLab **(do it)** we see that we can see our little planets!  Hurray!_____no_output_____Now we will go through the process of uploading to Sketchfab: **DO IT**_____no_output_____### Exercise

Do this activity with your planetary model.  First import into MeshLab and then upload to your Sketchfab account.

Things to consider:
1. Should you rescale anything in your model?  Why or why not?
1. What should you choose for the radii of your planets and star?  How do you justify this?
1. What should you choose for the texture maps?  What kind of planets and star do you "think" these are?_____no_output_____


# Notebook from sanggusti/PySyft
Path: packages/syft/examples/duet/information_flow_demo_splitnn_tenseal/Tutorial_Data_Owner.ipynb

import syft as sy
import torch
from torchvision import datasets
from torchvision import transforms
from collections import OrderedDict
import tenseal as ts_____no_output_____duet = sy.duet(loopback=True)♫♫♫ > DUET LIVE STATUS  -  Objects: 1  Requests: 0   Messages: 27  Request Handlers: 1
duet.requests.add_handler(action="accept")_____no_output_____

###  Checkpoint 1 : Now STOP and run the Data Scientist notebook until the same checkpoint._____no_output_____

mnist_mean = 0.1307
mnist_std = 0.3081
batch_size = 64
epochs = 10
lr = 0.1
sigma = 1.0
max_per_sample_grad_norm = 1.0
delta = 1e-5
root = "."
weights_filename = "mnist_cnn_weights.pt"
device = torch.device("cpu")

test_loader = torch.utils.data.DataLoader(
    datasets.MNIST(
        root,
        train=False,
        download=True,
        transform=transforms.Compose(
            [
                transforms.ToTensor(),
                transforms.Normalize((mnist_mean,), (mnist_std,)),
            ]
        ),
    ),
    batch_size=100,
    shuffle=True,
    num_workers = 1,
    pin_memory = True
)_____no_output_____from torch import nn

# this shouldn't be here, but can't make it work
class ConvolutionalBase(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.conv1 = nn.Conv2d(1, 16, 8, 2, padding=3)
        self.conv2 = nn.Conv2d(16, 32, 4, 2)

    def forward(self, x):
        x = nn.functional.relu(self.conv1(x))
        x = nn.functional.max_pool2d(x, 2, 1)
        x = nn.functional.relu(self.conv2(x))
        x = nn.functional.max_pool2d(x, 2, 1)
        x = x.view(-1, 32 * 4 * 4)
        return x_____no_output_____conv_base_names = duet.store["conv_base_names"].get()
conv_base_weights = duet.store["conv_base_weights"].get()_____no_output_____conv_base_state_dict = OrderedDict()

for conv_base_name, conv_base_weight in zip(conv_base_names, conv_base_weights):
    conv_base_state_dict[conv_base_name] = conv_base_weight

conv_base = ConvolutionalBase()
conv_base.load_state_dict(conv_base_state_dict)_____no_output_____secret_data, secret_labels = next(iter(test_loader))
test_batch_size = 1
offset = 20
data_sample = secret_data[offset: offset + test_batch_size]
label_sample = secret_labels[offset: offset + test_batch_size]

batch_size_ptr = sy.lib.python.Int(test_batch_size).send(duet, searchable=True, tags=["batch_size"])/home/tudor/anaconda3/envs/om_work/lib/python3.7/site-packages/torch/cuda/__init__.py:52: UserWarning: CUDA initialization: Found no NVIDIA driver on your system. Please check that you have an NVIDIA GPU and installed a driver from http://www.nvidia.com/Download/index.aspx (Triggered internally at  /pytorch/c10/cuda/CUDAFunctions.cpp:100.)
  return torch._C._cuda_getDeviceCount() > 0
intermediary_activation_data_sample = conv_base(data_sample)_____no_output_____def create_ctx():
    poly_mod_degree = 8192
    coeff_mod_bit_sizes = [40, 21, 21, 21, 21, 40]
    ctx = ts.context(ts.SCHEME_TYPE.CKKS, poly_mod_degree, -1, coeff_mod_bit_sizes)
    ctx.global_scale = 2 ** 21
    return ctx

def prepare_encrypted_activation(context, activation_sample):
    return ts.ckks_tensor(context, activation_sample.detach().numpy())_____no_output_____context = create_ctx()
ctx_ptr = context.send(duet, searchable=True, tags=["context"])_____no_output_____encrypted_activation = prepare_encrypted_activation(context, intermediary_activation_data_sample)
encrypted_activation_ptr = encrypted_activation.send(duet, searchable=True, tags=["encrypted_activation"])_____no_output_____duet.store.pandas_____no_output_____

###  Checkpoint 2 : Now STOP and run the Data Scientist notebook until the same checkpoint._____no_output_____

result = duet.store["result"].get(delete_obj=False)
result.link_context(context)

result = result.decrypt()_____no_output_____probs = torch.nn.functional.softmax(torch.tensor(result.tolist()), dim=1)
label_max = torch.argmax(probs, dim=1)

print(f"Maximum probability for label {label_max} with true_label {label_sample}")_____no_output_____

###  Checkpoint 3 : Well done!_____no_output_____# Congratulations!!! - Time to Join the Community!

Congratulations on completing this notebook tutorial! If you enjoyed this and would like to join the movement toward privacy preserving, decentralized ownership of AI and the AI supply chain (data), you can do so in the following ways!

### Star PySyft on GitHub

The easiest way to help our community is just by starring the Repos! This helps raise awareness of the cool tools we're building.

- [Star PySyft](https://github.com/OpenMined/PySyft)

### Join our Slack!

The best way to keep up to date on the latest advancements is to join our community! You can do so by filling out the form at [http://slack.openmined.org](http://slack.openmined.org). #lib_tenseal and #code_tenseal are the main channels for the TenSEAL project.

### Donate

If you don't have time to contribute to our codebase, but would still like to lend support, you can also become a Backer on our Open Collective. All donations go toward our web hosting and other community expenses such as hackathons and meetups!

[OpenMined's Open Collective Page](https://opencollective.com/openmined)_____no_output_____


# Notebook from harrow/computational_QM
Path: 2_tensor_networks/sol10_1_infinite_MPS.ipynb

<code>
import numpy as np
import scipy.sparse.linalg
import matplotlib.pyplot as plt
%matplotlib inline

import tfi_exact # for later comparison_____no_output_____import a_mps
import b_model
import c_tebd_____no_output_____# (changes compared to previous definitions are marked with comments)
# adjustments for a_mps

class InfiniteMPS(a_mps.MPS):
    """Represents an uniform (infinite) MPS."""

    def get_theta2(self, i):
        """Calculate effective two-site wave function on sites i,j=(i+1) in mixed canonical form.

        The returned array has legs ``vL, i, j, vR``."""
        j = (i + 1) % self.L  # <- changed here
        return np.tensordot(self.get_theta1(i), self.Bs[j], [2, 0])  # vL i [vR], [vL] j vR

    def get_chi(self):
        """Return bond dimensions."""
        return [self.Bs[i].shape[2] for i in range(self.L)] # <- changed the argument of range(...)

    def bond_expectation_value(self, op):
        """Calculate expectation values of a local operator at each bond."""
        result = []
        for i in range(self.L):  # <- changed here
            theta = self.get_theta2(i)  # vL i j vR
            op_theta = np.tensordot(op[i], theta, axes=[[2, 3], [1, 2]])
            # i j [i*] [j*], vL [i] [j] vR
            result.append(np.tensordot(theta.conj(), op_theta, [[0, 1, 2, 3], [2, 0, 1, 3]]))
            # [vL*] [i*] [j*] [vR*], [i] [j] [vL] [vR]
        return np.real_if_close(result)

    def entanglement_entropy(self):
        """Return the (von-Neumann) entanglement entropy for a bipartition at any of the bonds."""
        result = []
        for i in range(1, self.L+1):  # <- changed here
            # Note: Self.Ss[i] is left of self.Bs[i], while we consider bond "i" in bond_expectation_value and for the
            # updates to be right of site i, therefore we start i with 1 and take i % L.
            S = self.Ss[i % self.L].copy()  # <- changed here
            S[S < 1.e-20] = 0.  # 0*log(0) should give 0; avoid warning or NaN.
            S2 = S * S
            assert abs(np.linalg.norm(S) - 1.) < 1.e-14
            result.append(-np.sum(S2 * np.log(S2)))
        return np.array(result)

def init_infinite_spinup_MPS(L=2):
    """Return a product state with all spins up as an MPS"""
    B = np.zeros([1, 2, 1], np.float)
    B[0, 0, 0] = 1.
    S = np.ones([1], np.float)
    Bs = [B.copy() for i in range(L)]
    Ss = [S.copy() for i in range(L)]
    return InfiniteMPS(Bs, Ss)_____no_output_____# adjustments of b_model
class InfiniteTFIModel(b_model.TFIModel):
    def init_H_bonds(self):
        """Initialize `H_bonds` hamiltonian. Called by __init__()."""
        sx, sz, id = self.sigmax, self.sigmaz, self.id
        d = self.d
        H_list = []
        for i in range(self.L): # <- changed here: up to L
            gL = gR = 0.5 * self.g
            # changed here: removed if-cases
            H_bond = -self.J * np.kron(sx, sx) - gL * np.kron(sz, id) - gR * np.kron(id, sz)
            # H_bond has legs ``i, j, i*, j*``
            H_list.append(np.reshape(H_bond, [d, d, d, d]))
        self.H_bonds = H_list

    def energy(self, psi):
        return super().energy(psi) / self.L_____no_output_____# adjustments of c_tebd
def update_bond(psi, i, U_bond, chi_max, eps):
    """Apply `U_bond` acting on i,j=(i+1) to `psi`."""
    j = (i + 1) % psi.L  # <- changed here
    # construct theta matrix
    theta = psi.get_theta2(i)  # vL i j vR
    # apply U
    Utheta = np.tensordot(U_bond, theta, axes=([2, 3], [1, 2]))  # i j [i*] [j*], vL [i] [j] vR
    Utheta = np.transpose(Utheta, [2, 0, 1, 3])  # vL i j vR
    # split and truncate
    Ai, Sj, Bj = a_mps.split_truncate_theta(Utheta, chi_max, eps)   # the original split_truncate_theta
    # put back into MPS
    Gi = np.tensordot(np.diag(psi.Ss[i]**(-1)), Ai, axes=[1, 0])  # vL [vL*], [vL] i vC
    psi.Bs[i] = np.tensordot(Gi, np.diag(Sj), axes=[2, 0])  # vL i [vC], [vC] vC
    psi.Ss[j] = Sj  # vC
    psi.Bs[j] = Bj  # vC j vR

def run_TEBD(psi, U_bonds, N_steps, chi_max, eps):
    """Evolve the state `psi` for `N_steps` time steps with (first order) TEBD.

    The state psi is modified in place."""
    Nbonds = psi.L  # <- changed
    assert len(U_bonds) == Nbonds
    for n in range(N_steps):
        for k in [0, 1]:  # even, odd
            for i_bond in range(k, Nbonds, 2):
                update_bond(psi, i_bond, U_bonds[i_bond], chi_max, eps)
    # done

# adjusted a bit more of this function...
def example_TEBD_gs_infinite(J, g, N_steps=500, chi_max=30):
    L = 2  # unit cell of two sites
    print("infinite TEBD (imaginary time evolution)")
    print("J={J:.1f}, g={g:.2f}".format(J=J, g=g))
    model = InfiniteTFIModel(L, J=J, g=g) # use the new defined model
    psi = init_infinite_spinup_MPS(L) # and new InfiniteMPS
    for dt in [0.1, 0.01, 0.001, 1.e-4, 1.e-5]:
        U_bonds = c_tebd.calc_U_bonds(model, dt) # still the same
        run_TEBD(psi, U_bonds, N_steps=N_steps, chi_max=chi_max, eps=1.e-10)  # the new function!
        E = model.energy(psi)
        print("dt = {dt:.5f}: E = {E:.13f}".format(dt=dt, E=E))
    print("final bond dimensions: ", psi.get_chi())
    E_exact = tfi_exact.infinite_gs_energy(1., g) # infinite instead of finite
    print("Exact (analytic): E = {E:.13f}".format(E=E_exact))
    print("relative error: ", abs((E - E_exact) / E_exact))
    return E, psi, model_____no_output_____E, _, _ = example_TEBD_gs_infinite(1., 1.5, N_steps=100)infinite TEBD (imaginary time evolution)
J=1.0, g=1.50
dt = 0.10000: E = -1.6622376659950
dt = 0.01000: E = -1.6709745193387
dt = 0.00100: E = -1.6718222587090
dt = 0.00010: E = -1.6719075837948
dt = 0.00001: E = -1.6719161221699
final bond dimensions:  [21, 21]
Exact (analytic): E = -1.6719262215362
relative error:  6.040557413640872e-06
E, psi, _ = example_TEBD_gs_infinite(1., 1.0, N_steps=100)  # at the critical point: larger errorinfinite TEBD (imaginary time evolution)
J=1.0, g=1.00
dt = 0.10000: E = -1.2686637925848
dt = 0.01000: E = -1.2727299193540
dt = 0.00100: E = -1.2731198376954
dt = 0.00010: E = -1.2731590675618
dt = 0.00001: E = -1.2731629929455
final bond dimensions:  [30, 30]
Exact (analytic): E = -1.2732395447352
relative error:  6.012363499497619e-05
def eigvals_transfer_matrix(psi):
    chi = psi.Bs[0].shape[0]
    L = psi.L

    T = np.tensordot(psi.Bs[0], np.conj(psi.Bs[0]), axes=(1, 1))  # a,b,a*,b*
    T = T.transpose(0, 2, 1, 3)  # a,a*,b,b*
    for i in range(1, L):
        T = np.tensordot(T, psi.Bs[i], axes=(2, 0))  # a,a*,b*,i,b
        T = np.tensordot(T, np.conj(psi.Bs[i]), axes=([2, 3], [0,1]))  #a,a*,b,b*
    T = np.reshape(T, (chi**2, chi**2))

    # Obtain the 3 largest eigenvalues
    eigvals = scipy.sparse.linalg.eigs(T, k=3, which='LM', return_eigenvectors=False, ncv=20)
    return np.abs(eigvals)

eigvals_transfer_matrix(psi)
_____no_output_____gs = np.linspace(0.5, 1.5, 11)
TM_eigvals = []
for g in gs:
    E, psi, mod = example_TEBD_gs_infinite(1., g, N_steps=100)
    TM_eigvals.append(eigvals_transfer_matrix(psi))infinite TEBD (imaginary time evolution)
J=1.0, g=0.50
dt = 0.10000: E = -1.0633607863280
dt = 0.01000: E = -1.0635249098752
dt = 0.00100: E = -1.0635422497540
dt = 0.00010: E = -1.0635439872170
dt = 0.00001: E = -1.0635441609815
final bond dimensions:  [20, 20]
Exact (analytic): E = -1.0635444099734
relative error:  2.3411520705117052e-07
infinite TEBD (imaginary time evolution)
J=1.0, g=0.60
dt = 0.10000: E = -1.0918512402977
dt = 0.01000: E = -1.0921978585237
dt = 0.00100: E = -1.0922340686635
dt = 0.00010: E = -1.0922376979643
dt = 0.00001: E = -1.0922380609448
final bond dimensions:  [28, 28]
Exact (analytic): E = -1.0922385835545
relative error:  4.784757710215042e-07
infinite TEBD (imaginary time evolution)
J=1.0, g=0.70
dt = 0.10000: E = -1.1260911890582
dt = 0.01000: E = -1.1267518964978
dt = 0.00100: E = -1.1268198945470
dt = 0.00010: E = -1.1268267120569
dt = 0.00001: E = -1.1268273939305
final bond dimensions:  [30, 30]
Exact (analytic): E = -1.1268286675870
relative error:  1.1303018420928593e-06
infinite TEBD (imaginary time evolution)
J=1.0, g=0.80
dt = 0.10000: E = -1.1664701246078
dt = 0.01000: E = -1.1676630597552
dt = 0.00100: E = -1.1677829644954
dt = 0.00010: E = -1.1677949868939
dt = 0.00001: E = -1.1677961893753
final bond dimensions:  [30, 30]
Exact (analytic): E = -1.1678095085206
relative error:  1.1405237946322847e-05
infinite TEBD (imaginary time evolution)
J=1.0, g=0.90
dt = 0.10000: E = -1.2133058065659
dt = 0.01000: E = -1.2155241266863
dt = 0.00100: E = -1.2157380849118
dt = 0.00010: E = -1.2157595227144
dt = 0.00001: E = -1.2157616668324
final bond dimensions:  [30, 30]
Exact (analytic): E = -1.2160009141097
relative error:  0.0001967492578131006
infinite TEBD (imaginary time evolution)
J=1.0, g=1.00
dt = 0.10000: E = -1.2686637925848
dt = 0.01000: E = -1.2727299193540
dt = 0.00100: E = -1.2731198376954
dt = 0.00010: E = -1.2731590675618
dt = 0.00001: E = -1.2731629929455
final bond dimensions:  [30, 30]
Exact (analytic): E = -1.2732395447352
relative error:  6.012363499497619e-05
infinite TEBD (imaginary time evolution)
J=1.0, g=1.10
dt = 0.10000: E = -1.3364000301965
dt = 0.01000: E = -1.3422325671124
dt = 0.00100: E = -1.3427938523044
dt = 0.00010: E = -1.3428503840516
dt = 0.00001: E = -1.3428560413634
final bond dimensions:  [30, 30]
Exact (analytic): E = -1.3428640227251
relative error:  5.943536757133757e-06
infinite TEBD (imaginary time evolution)
J=1.0, g=1.20
dt = 0.10000: E = -1.4119124670645
dt = 0.01000: E = -1.4188663486286
dt = 0.00100: E = -1.4195362243599
dt = 0.00010: E = -1.4196036808059
dt = 0.00001: E = -1.4196104312933
final bond dimensions:  [30, 30]
Exact (analytic): E = -1.4196192748982
relative error:  6.229561048794512e-06
infinite TEBD (imaginary time evolution)
J=1.0, g=1.30
dt = 0.10000: E = -1.4922537566071
dt = 0.01000: E = -1.4999848030050
dt = 0.00100: E = -1.5007311473366
dt = 0.00010: E = -1.5008062899728
dt = 0.00001: E = -1.5008138095229
final bond dimensions:  [28, 28]
Exact (analytic): E = -1.5008232436750
relative error:  6.285984838964919e-06
infinite TEBD (imaginary time evolution)
J=1.0, g=1.40
dt = 0.10000: E = -1.5759838405825
dt = 0.01000: E = -1.5842860113434
dt = 0.00100: E = -1.5850894924141
dt = 0.00010: E = -1.5851703745563
dt = 0.00001: E = -1.5851784683798
final bond dimensions:  [24, 24]
Exact (analytic): E = -1.5851883000540
relative error:  6.20221218499629e-06
infinite TEBD (imaginary time evolution)
J=1.0, g=1.50
dt = 0.10000: E = -1.6622376659950
dt = 0.01000: E = -1.6709745193387
dt = 0.00100: E = -1.6718222587090
dt = 0.00010: E = -1.6719075837948
dt = 0.00001: E = -1.6719161221699
final bond dimensions:  [21, 21]
Exact (analytic): E = -1.6719262215362
relative error:  6.040557413640872e-06
TM_eigvals = np.array(TM_eigvals)
plt.figure()
for i in range(3):
    plt.plot(gs, TM_eigvals[:, i])
plt.xlabel('$g/J$')
plt.ylabel('eigenvals. of transfer matrix')
plt.show()_____no_output_____
</code>

## Interpretation
The transfermatrix appears repeatedly in expectation values $<\psi|A_i|\psi> $ and correlation functions $<\psi|A_i B_j |\psi>$. If the state is in (right) canonical form, the largest eigenvalues is 1, with $\delta_{cc'}$ as the corresponding eigenvector - the equality $T_{aa',cc'} \delta_{cc'} = \delta_{aa'}$ follows directly from the orthogonality of the B's; similarly $\Lambda_a^2 \delta_{aa'} is a left eigenvector.

For a pure state, the eigenvalue $1$ is unique, but for g < 1, $e^{-\tau H}$ yields the states $ |\rightarrow \rightarrow \dots > $ and $|\leftarrow \leftarrow \dots>$ (each + quantum fluctuations) with equal weight, i.e., not a "pure" state,
hence the degeneracy of the eigenvalue 1.

The second largest eigenvalue determines the correlation length: we can write the correlation functions as $ <A_i B_j> = L_A  T^{|j-i|/L} R_B $, where $L_A$ and $R_B$ are left and right parts of the corresponding tensor network.
Hence, the second largest eigenvalue determines the decay of
the connected part $<A_i B_j> - <A_i> <B_j>$.
At the critical point, the correlation length diverges, which is equivalent to the second largest eigenvalues going to 1 (at least if we increase the bond dimension enough, a finite $\chi$ introduces a finite correlation length).
_____no_output_____


# Notebook from xiaoyanLi629/single_cell_data_analysis
Path: protein_1_analysis/Auto_encoder_no_norm_RNA_pro_feat_1_subgroup_s1d9_eucli_s3d1_5.ipynb

<code>
import anndata as ad
import gc
import sys
from scipy.sparse import csc_matrix
from sklearn.model_selection import train_test_split
from sklearn.decomposition import TruncatedSVD
from sklearn.linear_model import LinearRegression
import random
import numpy as np
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import TensorDataset, DataLoader
import matplotlib.pyplot as plt
import pickle
import heapq
import matplotlib
import os
import umap
from sklearn.decomposition import PCA
from tqdm import tqdm
from numpy import linalg as LA
import pickle_____no_output_____device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(device)
random.seed(1)
np.random.seed(1)
torch.manual_seed(1)

batch_size = 50000
pred_start = 1
pred_end = 2cuda
train_mod1_file = 'phase2_private_data/predict_modality/openproblems_bmmc_cite_phase2_rna/openproblems_bmmc_cite_phase2_rna.censor_dataset.output_train_mod1.h5ad'
train_mod2_file = 'phase2_private_data/predict_modality/openproblems_bmmc_cite_phase2_rna/openproblems_bmmc_cite_phase2_rna.censor_dataset.output_train_mod2.h5ad'
test_mod1_file = 'phase2_private_data/predict_modality/openproblems_bmmc_cite_phase2_rna/openproblems_bmmc_cite_phase2_rna.censor_dataset.output_test_mod1.h5ad'
test_mod2_file = 'phase2_private_data/predict_modality/openproblems_bmmc_cite_phase2_rna/openproblems_bmmc_cite_phase2_rna.censor_dataset.output_test_mod2.h5ad'_____no_output_____input_train_mod1 = ad.read_h5ad(train_mod1_file)
input_train_mod2 = ad.read_h5ad(train_mod2_file)
input_test_mod1 = ad.read_h5ad(test_mod1_file)
input_test_mod2 = ad.read_h5ad(test_mod2_file)_____no_output_____print(input_train_mod1)
print(input_train_mod2)
print(input_test_mod1)
print(input_test_mod2)AnnData object with n_obs × n_vars = 66175 × 13953
    obs: 'batch', 'size_factors'
    var: 'gene_ids', 'feature_types'
    uns: 'dataset_id', 'organism'
    layers: 'counts'
AnnData object with n_obs × n_vars = 66175 × 134
    obs: 'batch', 'size_factors'
    var: 'feature_types'
    uns: 'dataset_id', 'organism'
    layers: 'counts'
AnnData object with n_obs × n_vars = 1000 × 13953
    obs: 'batch', 'size_factors'
    var: 'gene_ids', 'feature_types'
    uns: 'dataset_id', 'organism'
    layers: 'counts'
AnnData object with n_obs × n_vars = 1000 × 134
    obs: 'batch', 'size_factors'
    var: 'feature_types'
    uns: 'dataset_id', 'organism'
    layers: 'counts'
print(sorted(set(input_train_mod1.obs['batch'])))
print(sorted(set(input_train_mod2.obs['batch'])))['s1d1', 's1d2', 's1d3', 's2d1', 's2d4', 's2d5', 's3d1', 's3d6', 's3d7']
['s1d1', 's1d2', 's1d3', 's2d1', 's2d4', 's2d5', 's3d1', 's3d6', 's3d7']
print(sorted(set(input_test_mod1.obs['batch'])))
print(sorted(set(input_test_mod2.obs['batch'])))['s4d1', 's4d8', 's4d9']
['s4d1', 's4d8', 's4d9']
RNA_s1d1 = input_train_mod1[input_train_mod1.obs["batch"] == "s1d1", :]
RNA_s1d2 = input_train_mod1[input_train_mod1.obs["batch"] == "s1d2", :]
RNA_s1d3 = input_train_mod1[input_train_mod1.obs["batch"] == "s1d3", :]
RNA_s2d1 = input_train_mod1[input_train_mod1.obs["batch"] == "s2d1", :]
RNA_s2d4 = input_train_mod1[input_train_mod1.obs["batch"] == "s2d4", :]
RNA_s2d5 = input_train_mod1[input_train_mod1.obs["batch"] == "s2d5", :]

RNA_s3d1 = input_train_mod1[input_train_mod1.obs["batch"] == "s3d1", :]
RNA_s3d6 = input_train_mod1[input_train_mod1.obs["batch"] == "s3d6", :]
RNA_s3d7 = input_train_mod1[input_train_mod1.obs["batch"] == "s3d7", :]_____no_output_____pro_s1d1 = input_train_mod2[input_train_mod2.obs["batch"] == "s1d1", :]
pro_s1d2 = input_train_mod2[input_train_mod2.obs["batch"] == "s1d2", :]
pro_s1d3 = input_train_mod2[input_train_mod2.obs["batch"] == "s1d3", :]
pro_s2d1 = input_train_mod2[input_train_mod2.obs["batch"] == "s2d1", :]
pro_s2d4 = input_train_mod2[input_train_mod2.obs["batch"] == "s2d4", :]
pro_s2d5 = input_train_mod2[input_train_mod2.obs["batch"] == "s2d5", :]

pro_s3d1 = input_train_mod2[input_train_mod2.obs["batch"] == "s3d1", :]
pro_s3d6 = input_train_mod2[input_train_mod2.obs["batch"] == "s3d6", :]
pro_s3d7 = input_train_mod2[input_train_mod2.obs["batch"] == "s3d7", :]_____no_output_____RNA_s4d1 = input_test_mod1[input_test_mod1.obs["batch"] == "s4d1", :]
RNA_s4d8 = input_test_mod1[input_test_mod1.obs["batch"] == "s4d8", :]
RNA_s4d9 = input_test_mod1[input_test_mod1.obs["batch"] == "s4d9", :]_____no_output_____pro_s4d1 = input_test_mod2[input_test_mod2.obs["batch"] == "s4d1", :]
pro_s4d8 = input_test_mod2[input_test_mod2.obs["batch"] == "s4d8", :]
pro_s4d9 = input_test_mod2[input_test_mod2.obs["batch"] == "s4d9", :]_____no_output_____RNA_s1d1 = RNA_s1d1.X.toarray()
RNA_s1d2 = RNA_s1d2.X.toarray()
RNA_s1d3 = RNA_s1d3.X.toarray()
RNA_s2d1 = RNA_s2d1.X.toarray()
RNA_s2d4 = RNA_s2d4.X.toarray()
RNA_s2d5 = RNA_s2d5.X.toarray()

RNA_s3d1 = RNA_s3d1.X.toarray()
RNA_s3d6 = RNA_s3d6.X.toarray()
RNA_s3d7 = RNA_s3d7.X.toarray()_____no_output_____pro_s1d1 = pro_s1d1.X.toarray()[:, pred_start:pred_end]
pro_s1d2 = pro_s1d2.X.toarray()[:, pred_start:pred_end]
pro_s1d3 = pro_s1d3.X.toarray()[:, pred_start:pred_end]
pro_s2d1 = pro_s2d1.X.toarray()[:, pred_start:pred_end]
pro_s2d4 = pro_s2d4.X.toarray()[:, pred_start:pred_end]
pro_s2d5 = pro_s2d5.X.toarray()[:, pred_start:pred_end]

pro_s3d1 = pro_s3d1.X.toarray()[:, pred_start:pred_end]
pro_s3d6 = pro_s3d6.X.toarray()[:, pred_start:pred_end]
pro_s3d7 = pro_s3d7.X.toarray()[:, pred_start:pred_end]_____no_output_____RNA_s4d1 = RNA_s4d1.X.toarray()
RNA_s4d8 = RNA_s4d8.X.toarray()
RNA_s4d9 = RNA_s4d9.X.toarray()_____no_output_____pro_s4d1 = pro_s4d1.X.toarray()[:, pred_start:pred_end]
pro_s4d8 = pro_s4d8.X.toarray()[:, pred_start:pred_end]
pro_s4d9 = pro_s4d9.X.toarray()[:, pred_start:pred_end]_____no_output_____print(RNA_s1d1.shape)
print(pro_s1d1.shape)(4721, 13953)
(4721, 1)
batch_s1d1 = input_train_mod1[input_train_mod1.obs["batch"] == "s1d1", :].obs["batch"]
batch_s1d2 = input_train_mod1[input_train_mod1.obs["batch"] == "s1d2", :].obs["batch"]
batch_s1d3 = input_train_mod1[input_train_mod1.obs["batch"] == "s1d3", :].obs["batch"]
batch_s2d1 = input_train_mod1[input_train_mod1.obs["batch"] == "s2d1", :].obs["batch"]
batch_s2d4 = input_train_mod1[input_train_mod1.obs["batch"] == "s2d4", :].obs["batch"]
batch_s2d5 = input_train_mod1[input_train_mod1.obs["batch"] == "s2d5", :].obs["batch"]

batch_s3d1 = input_train_mod1[input_train_mod1.obs["batch"] == "s3d1", :].obs["batch"]
batch_s3d6 = input_train_mod1[input_train_mod1.obs["batch"] == "s3d6", :].obs["batch"]
batch_s3d7 = input_train_mod1[input_train_mod1.obs["batch"] == "s3d7", :].obs["batch"]

batch_s4d1 = input_test_mod1[input_test_mod1.obs["batch"] == "s4d1", :].obs["batch"]
batch_s4d8 = input_test_mod1[input_test_mod1.obs["batch"] == "s4d8", :].obs["batch"]
batch_s4d9 = input_test_mod1[input_test_mod1.obs["batch"] == "s4d9", :].obs["batch"]_____no_output_____batch_train = list(batch_s1d1)+list(batch_s1d2)+list(batch_s1d3)+list(batch_s2d1)+list(batch_s2d4)+list(batch_s2d5)
batch_val = list(batch_s3d1)+list(batch_s3d6)+list(batch_s3d7)
batch_test = list(batch_s4d1)+list(batch_s4d8)+list(batch_s4d9)_____no_output_____# train_input = [RNA_s1d1, RNA_s1d2, RNA_s1d3, RNA_s2d1, RNA_s2d4, RNA_s2d5]
# train_output = [pro_s1d1, pro_s1d2, pro_s1d3, pro_s2d1, pro_s2d4, pro_s2d5]

train_input = [RNA_s3d1]
train_output = [pro_s3d1]

val_input = [RNA_s3d1, RNA_s3d6, RNA_s3d7]
val_output = [pro_s3d1, pro_s3d6, pro_s3d7]

test_input = [RNA_s4d9]
test_output = [pro_s4d9]_____no_output_____train_input = train_input
train_output = train_output_____no_output_____train_input = np.concatenate(train_input, axis=0)
val_input = np.concatenate(val_input, axis=0)
test_input = np.concatenate(test_input, axis=0)

train_output = np.concatenate(train_output, axis=0)
val_output = np.concatenate(val_output, axis=0)
test_output = np.concatenate(test_output, axis=0)_____no_output_____RNA_s1d1 = torch.from_numpy(RNA_s1d1)
RNA_s1d2 = torch.from_numpy(RNA_s1d2)
RNA_s1d3 = torch.from_numpy(RNA_s1d3)
RNA_s2d1 = torch.from_numpy(RNA_s2d1)
RNA_s2d4 = torch.from_numpy(RNA_s2d4)
RNA_s2d5 = torch.from_numpy(RNA_s2d5)
RNA_s3d1 = torch.from_numpy(RNA_s3d1)
RNA_s3d6 = torch.from_numpy(RNA_s3d6)
RNA_s3d7 = torch.from_numpy(RNA_s3d7)
RNA_s4d1 = torch.from_numpy(RNA_s4d1)
RNA_s4d8 = torch.from_numpy(RNA_s4d8)
RNA_s4d9 = torch.from_numpy(RNA_s4d9)_____no_output_____pro_s1d1 = torch.from_numpy(pro_s1d1)
pro_s1d2 = torch.from_numpy(pro_s1d2)
pro_s1d3 = torch.from_numpy(pro_s1d3)
pro_s2d1 = torch.from_numpy(pro_s2d1)
pro_s2d4 = torch.from_numpy(pro_s2d4)
pro_s2d5 = torch.from_numpy(pro_s2d5)
pro_s3d1 = torch.from_numpy(pro_s3d1)
pro_s3d6 = torch.from_numpy(pro_s3d6)
pro_s3d7 = torch.from_numpy(pro_s3d7)
pro_s4d1 = torch.from_numpy(pro_s4d1)
pro_s4d8 = torch.from_numpy(pro_s4d8)
pro_s4d9 = torch.from_numpy(pro_s4d9)_____no_output_____train_input = torch.from_numpy(train_input)
val_input = torch.from_numpy(val_input)
test_input = torch.from_numpy(test_input)

train_output = torch.from_numpy(train_output)
val_output = torch.from_numpy(val_output)
test_output = torch.from_numpy(test_output)_____no_output_____print(train_input.shape)
print(val_input.shape)
print(test_input.shape)

print(train_output.shape)
print(val_output.shape)
print(test_output.shape)torch.Size([8582, 13953])
torch.Size([28921, 13953])
torch.Size([459, 13953])
torch.Size([8582, 1])
torch.Size([28921, 1])
torch.Size([459, 1])
def top_samples(source, target, num):
    distances = LA.norm(source-target, axis=1)
    temp_list = []
    for i in range(len(distances)):
        temp_list.append([distances[i], i])
    temp_list.sort()
    return [ele[1] for ele in temp_list[:num]]_____no_output_____nn_num = 5_____no_output_____input_near_ind_list = []
output_near_ind_list = []

for i in tqdm(range(test_input.shape[0])):
    near_neig_ind = top_samples(test_input[i], train_input, nn_num)
    input_near_ind_list.append(train_input[near_neig_ind])
    output_near_ind_list.append(train_output[near_neig_ind])

input_near = np.concatenate(input_near_ind_list, axis= 0)
output_near = np.concatenate(output_near_ind_list, axis= 0)100%|██████████| 459/459 [01:38<00:00,  4.66it/s]
def top_samples_distances(source, target, num):
    distances = LA.norm(source-target, axis=1)
    temp_list = []
    for i in range(len(distances)):
        temp_list.append([distances[i], i])
    temp_list.sort()
    return sum([ele[0] for ele in temp_list[:num]])_____no_output_____train_batches = [RNA_s1d1, RNA_s1d2, RNA_s1d3, RNA_s2d1, RNA_s2d4, RNA_s2d5, RNA_s3d1, RNA_s3d6, RNA_s3d7, RNA_s4d1, RNA_s4d8, RNA_s4d9]
train_batches_names = ['RNA_s1d1', 'RNA_s1d2', 'RNA_s1d3', 'RNA_s2d1', 'RNA_s2d4', 'RNA_s2d5', 'RNA_s3d1', 'RNA_s3d6', 'RNA_s3d7', 'RNA_s4d1', 'RNA_s4d8', 'RNA_s4d9']

for j in range(len(train_batches)):
    dis_sum = 0
    for i in range(RNA_s4d9.shape[0]):
        near_dis_sum = top_samples_distances(RNA_s4d9[i], train_batches[j], nn_num)
        dis_sum = dis_sum + near_dis_sum
    print('batch:', train_batches_names[j], 'distance sum:', dis_sum)batch: RNA_s1d1 distance sum: 98918.28414344788
batch: RNA_s1d2 distance sum: 100879.445936203
batch: RNA_s1d3 distance sum: 97331.0188961029
batch: RNA_s2d1 distance sum: 99191.10357284546
batch: RNA_s2d4 distance sum: 100811.46552658081
batch: RNA_s2d5 distance sum: 99900.19089126587
batch: RNA_s3d1 distance sum: 107954.9629611969
batch: RNA_s3d6 distance sum: 99665.58183288574
batch: RNA_s3d7 distance sum: 100274.4892578125
batch: RNA_s4d1 distance sum: 105758.55142784119
batch: RNA_s4d8 distance sum: 109721.68766975403
batch: RNA_s4d9 distance sum: 83305.21807479858
# with open(r"input_near_eucli.pickle", "wb") as output_file:
#     pickle.dump(input_near, output_file)

# with open(r"output_near_eucli.pickle", "wb") as output_file:
#     pickle.dump(output_near, output_file)_____no_output_____# with open(r"input_near_eucli.pickle", "rb") as input_file:
#     input_near = pickle.load(input_file)

# with open(r"output_near_eucli.pickle", "rb") as input_file:
#     output_near = pickle.load(input_file)_____no_output_____RNA_s1d1 = RNA_s1d1.float()
RNA_s1d2 = RNA_s1d2.float()
RNA_s1d3 = RNA_s1d3.float()
RNA_s2d1 = RNA_s2d1.float()
RNA_s2d4 = RNA_s2d4.float()
RNA_s2d5 = RNA_s2d5.float()
RNA_s3d1 = RNA_s3d1.float()
RNA_s3d6 = RNA_s3d6.float()
RNA_s3d7 = RNA_s3d7.float()
RNA_s4d1 = RNA_s4d1.float()
RNA_s4d8 = RNA_s4d8.float()
RNA_s4d9 = RNA_s4d9.float()

pro_s1d1 = pro_s1d1.float()
pro_s1d2 = pro_s1d2.float()
pro_s1d3 = pro_s1d3.float()
pro_s2d1 = pro_s2d1.float()
pro_s2d4 = pro_s2d4.float()
pro_s2d5 = pro_s2d5.float()
pro_s3d1 = pro_s3d1.float()
pro_s3d6 = pro_s3d6.float()
pro_s3d7 = pro_s3d7.float()
pro_s4d1 = pro_s4d1.float()
pro_s4d8 = pro_s4d8.float()
pro_s4d9 = pro_s4d9.float()_____no_output_____train_input = train_input.float()
val_input = val_input.float()
test_input = test_input.float()

train_output = train_output.float()
val_output = val_output.float()
test_output = test_output.float()_____no_output_____# input_near = input_near.float()
# output_near = output_near.float()_____no_output_____RNA_s1d1 = RNA_s1d1.to(device)
RNA_s1d2 = RNA_s1d2.to(device)
RNA_s1d3 = RNA_s1d3.to(device)
RNA_s2d1 = RNA_s2d1.to(device)
RNA_s2d4 = RNA_s2d4.to(device)
RNA_s2d5 = RNA_s2d5.to(device)
RNA_s3d1 = RNA_s3d1.to(device)
RNA_s3d6 = RNA_s3d6.to(device)
RNA_s3d7 = RNA_s3d7.to(device)
RNA_s4d1 = RNA_s4d1.to(device)
RNA_s4d8 = RNA_s4d8.to(device)
RNA_s4d9 = RNA_s4d9.to(device)

pro_s1d1 = pro_s1d1.to(device)
pro_s1d2 = pro_s1d2.to(device)
pro_s1d3 = pro_s1d3.to(device)
pro_s2d1 = pro_s2d1.to(device)
pro_s2d4 = pro_s2d4.to(device)
pro_s2d5 = pro_s2d5.to(device)
pro_s3d1 = pro_s3d1.to(device)
pro_s3d6 = pro_s3d6.to(device)
pro_s3d7 = pro_s3d7.to(device)
pro_s4d1 = pro_s4d1.to(device)
pro_s4d8 = pro_s4d8.to(device)
pro_s4d9 = pro_s4d9.to(device)_____no_output_____train_input = train_input.to(device)
val_input = val_input.to(device)
test_input = test_input.to(device)

train_output = train_output.to(device)
val_output = val_output.to(device)
test_output = test_output.to(device)_____no_output_____# input_near = torch.from_numpy(input_near)
# output_near = torch.from_numpy(output_near)_____no_output_____input_near = torch.from_numpy(input_near)
output_near = torch.from_numpy(output_near)

input_near = input_near.to(device)
output_near = output_near.to(device)_____no_output_____input_feature = RNA_s1d1.shape[1]_____no_output_____# auto-encoder model
# base model
class Autoencoder(nn.Module):
    def __init__(self):
        super(Autoencoder, self).__init__()
        self.linear1 = nn.Linear(input_feature, input_feature//16)
        self.linear2 = nn.Linear(input_feature//2, input_feature//4)
        self.linear3 = nn.Linear(input_feature//4, input_feature//8)
        self.linear4 = nn.Linear(input_feature//8, input_feature//16)

        self.linear5 = nn.Linear(input_feature//16, 1)
        self.relu = nn.ReLU()
        self.leakyrelu = nn.LeakyReLU(0.1)
        self.dropout = nn.Dropout(0.15)

        self.linear = nn.Linear(input_feature, 1)

    def forward(self, x):
        x = self.linear1(x)
        x = self.relu(x)
        x = self.dropout(x)

#         x = self.linear2(x)
#         x = self.relu(x)
#         x = self.dropout(x)

#         x = self.linear3(x)
#         x = self.relu(x)

#         x = self.linear4(x)
#         x = self.relu(x)

        output = self.linear5(x)

        return output.float()_____no_output_____PATH = 'No_norm_model_RNA_pro'_____no_output_____def fit(num_epochs, model, loss_fn, RNA, pro):
    train_ds = TensorDataset(RNA, pro)
    train_dl = DataLoader(train_ds, batch_size= batch_size, shuffle=True)
    val_best = float('inf')
    train_pred_output = []
    val_pred_output = []
    for epoch in range(num_epochs):
        for x,y in train_dl:
            model = model.train()
            pred = model(x)
            loss = loss_fn(pred, y)
            loss.backward()
            opt.step()
            opt.zero_grad()

        if epoch % 100 == 0:
            model = model.eval()

            near_pred = model(input_near)
            near_loss = loss_fn(near_pred, output_near)
            near_loss = torch.sqrt(near_loss)
            near_loss = near_loss.cpu().detach().numpy()

            s1d1_pred = model(RNA_s1d1)
            s1d1_loss = loss_fn(s1d1_pred, pro_s1d1)
            s1d1_loss = torch.sqrt(s1d1_loss)
            s1d1_loss = s1d1_loss.cpu().detach().numpy()

            s1d2_pred = model(RNA_s1d2)
            s1d2_loss = loss_fn(s1d2_pred, pro_s1d2)
            s1d2_loss = torch.sqrt(s1d2_loss)
            s1d2_loss = s1d2_loss.cpu().detach().numpy()

            s1d3_pred = model(RNA_s1d3)
            s1d3_loss = loss_fn(s1d3_pred, pro_s1d3)
            s1d3_loss = torch.sqrt(s1d3_loss)
            s1d3_loss = s1d3_loss.cpu().detach().numpy()

            s2d1_pred = model(RNA_s2d1)
            s2d1_loss = loss_fn(s2d1_pred, pro_s2d1)
            s2d1_loss = torch.sqrt(s2d1_loss)
            s2d1_loss = s2d1_loss.cpu().detach().numpy()

            s2d4_pred = model(RNA_s2d4)
            s2d4_loss = loss_fn(s2d4_pred, pro_s2d4)
            s2d4_loss = torch.sqrt(s2d4_loss)
            s2d4_loss = s2d4_loss.cpu().detach().numpy()

            s2d5_pred = model(RNA_s2d5)
            s2d5_loss = loss_fn(s2d5_pred, pro_s2d5)
            s2d5_loss = torch.sqrt(s2d5_loss)
            s2d5_loss = s2d5_loss.cpu().detach().numpy()

            s3d1_pred = model(RNA_s3d1)
            s3d1_loss = loss_fn(s3d1_pred, pro_s3d1)
            s3d1_loss = torch.sqrt(s3d1_loss)
            s3d1_loss = s3d1_loss.cpu().detach().numpy()

            s3d6_pred = model(RNA_s3d6)
            s3d6_loss = loss_fn(s3d6_pred, pro_s3d6)
            s3d6_loss = torch.sqrt(s3d6_loss)
            s3d6_loss = s3d6_loss.cpu().detach().numpy()

            s3d7_pred = model(RNA_s3d7)
            s3d7_loss = loss_fn(s3d7_pred, pro_s3d7)
            s3d7_loss = torch.sqrt(s3d7_loss)
            s3d7_loss = s3d7_loss.cpu().detach().numpy()

            s4d1_pred = model(RNA_s4d1)
            s4d1_loss = loss_fn(s4d1_pred, pro_s4d1)
            s4d1_loss = torch.sqrt(s4d1_loss)
            s4d1_loss = s4d1_loss.cpu().detach().numpy()

            s4d8_pred = model(RNA_s4d8)
            s4d8_loss = loss_fn(s4d8_pred, pro_s4d8)
            s4d8_loss = torch.sqrt(s4d8_loss)
            s4d8_loss = s4d8_loss.cpu().detach().numpy()

            s4d9_pred = model(RNA_s4d9)
            s4d9_loss = loss_fn(s4d9_pred, pro_s4d9)
            s4d9_loss = torch.sqrt(s4d9_loss)
            s4d9_loss = s4d9_loss.cpu().detach().numpy()

            test_loss = ((s4d1_loss**2*s4d1_pred.shape[0] + s4d8_loss**2*s4d8_pred.shape[0] + s4d9_loss**2*s4d9_pred.shape[0])/(s4d1_pred.shape[0]+s4d8_pred.shape[0]+s4d9_pred.shape[0]))**0.5

            print('Epoch ', epoch, 'near loss:', near_loss, 's1d1: ', s1d1_loss, 's1d2: ', s1d2_loss, 's1d3: ', s1d3_loss, 's2d1:', s2d1_loss, 's2d4:',
                  s2d4_loss, 's2d5:', s2d5_loss, 's3d1:', s3d1_loss, 's3d6:', s3d6_loss, 's3d7:', s3d7_loss, 's4d1:', s4d1_loss, 's4d8:', s4d8_loss, 's4d9:', s4d9_loss, 'test loss:', test_loss)

        if epoch % 1000 == 0:
            figure, ax = plt.subplots(1, 13, figsize = (33, 3))
            for i in range(1):

                ax[0].scatter(output_near.cpu().detach().numpy()[:, 0], near_pred.cpu().detach().numpy()[:, 0], c='purple', s = 30, alpha=0.05)
                ax[0].plot([-1.5, 3], [-1.5, 3], 'k-')
                ax[0].set_title('Nearest neighbor result')
                ax[0].set_xlabel('true')
                ax[0].set_ylabel('pred')

                ax[1].scatter(pro_s1d1.cpu().detach().numpy()[:, 0], s1d1_pred.cpu().detach().numpy()[:, 0], c='blue', s = 30, alpha=0.05)
                ax[1].plot([-1.5, 3], [-1.5, 3], 'k-')
                ax[1].set_title('s1d1 result')
                ax[1].set_xlabel('true')
                ax[1].set_ylabel('pred')

                ax[2].scatter(pro_s1d2.cpu().detach().numpy()[:, 0], s1d2_pred.cpu().detach().numpy()[:, 0], c='blue', s = 30, alpha=0.05)
                ax[2].plot([-1.5, 3], [-1.5, 3], 'k-')
                ax[2].set_title('s1d2 result')
                ax[2].set_xlabel('true')
                ax[2].set_ylabel('pred')

                ax[3].scatter(pro_s1d3.cpu().detach().numpy()[:, 0], s1d3_pred.cpu().detach().numpy()[:, 0], c='blue', s = 30, alpha=0.05)
                ax[3].plot([-1.5, 3], [-1.5, 3], 'k-')
                ax[3].set_title('s1d3 result')
                ax[3].set_xlabel('true')
                ax[3].set_ylabel('pred')

                ax[4].scatter(pro_s2d1.cpu().detach().numpy()[:, 0], s2d1_pred.cpu().detach().numpy()[:, 0], c='red', s = 30, alpha=0.05)
                ax[4].plot([-1.5, 3], [-1.5, 3], 'k-')
                ax[4].set_title('s2d1 result')
                ax[4].set_xlabel('true')
                ax[4].set_ylabel('pred')

                ax[5].scatter(pro_s2d4.cpu().detach().numpy()[:, 0], s2d4_pred.cpu().detach().numpy()[:, 0], c='red', s = 30, alpha=0.05)
                ax[5].plot([-1.5, 3], [-1.5, 3], 'k-')
                ax[5].set_title('s2d4 result')
                ax[5].set_xlabel('true')
                ax[5].set_ylabel('pred')

                ax[6].scatter(pro_s2d5.cpu().detach().numpy()[:, 0], s2d5_pred.cpu().detach().numpy()[:, 0], c='red', s = 30, alpha=0.05)
                ax[6].plot([-1.5, 3], [-1.5, 3], 'k-')
                ax[6].set_title('s2d5 result')
                ax[6].set_xlabel('true')
                ax[6].set_ylabel('pred')

                ax[7].scatter(pro_s3d1.cpu().detach().numpy()[:, 0], s3d1_pred.cpu().detach().numpy()[:, 0], c='orange', s = 30, alpha=0.05)
                ax[7].plot([-1.5, 3], [-1.5, 3], 'k-')
                ax[7].set_title('s3d1 result')
                ax[7].set_xlabel('true')
                ax[7].set_ylabel('pred')

                ax[8].scatter(pro_s3d6.cpu().detach().numpy()[:, 0], s3d6_pred.cpu().detach().numpy()[:, 0], c='orange', s = 30, alpha=0.05)
                ax[8].plot([-1.5, 3], [-1.5, 3], 'k-')
                ax[8].set_title('s3d6 result')
                ax[8].set_xlabel('true')
                ax[8].set_ylabel('pred')

                ax[9].scatter(pro_s3d7.cpu().detach().numpy()[:, 0], s3d7_pred.cpu().detach().numpy()[:, 0], c='orange', s = 30, alpha=0.05)
                ax[9].plot([-1.5, 3], [-1.5, 3], 'k-')
                ax[9].set_title('s3d7 result')
                ax[9].set_xlabel('true')
                ax[9].set_ylabel('pred')

                ax[10].scatter(pro_s4d1.cpu().detach().numpy()[:, 0], s4d1_pred.cpu().detach().numpy()[:, 0], c='green', s = 30, alpha=0.05)
                ax[10].plot([-1.5, 3], [-1.5, 3], 'k-')
                ax[10].set_title('s4d1 result')
                ax[10].set_xlabel('true')
                ax[10].set_ylabel('pred')

                ax[11].scatter(pro_s4d8.cpu().detach().numpy()[:, 0], s4d8_pred.cpu().detach().numpy()[:, 0], c='green', s = 30, alpha=0.05)
                ax[11].plot([-1.5, 3], [-1.5, 3], 'k-')
                ax[11].set_title('s4d8 result')
                ax[11].set_xlabel('true')
                ax[11].set_ylabel('pred')

                ax[12].scatter(pro_s4d9.cpu().detach().numpy()[:, 0], s4d9_pred.cpu().detach().numpy()[:, 0], c='green', s = 30, alpha=0.05)
                ax[12].plot([-1.5, 3], [-1.5, 3], 'k-')
                ax[12].set_title('s4d9 result')
                ax[12].set_xlabel('true')
                ax[12].set_ylabel('pred')

            plt.show()
    return_____no_output_____num_epochs = 2000
learning_rate = 0.01
latent_dim = 50
loss_fn = F.mse_loss
batch_size = 50000

def seed_everything(seed=1234):
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.backends.cudnn.deterministic = True

seed_everything()

model = Autoencoder()
# model.load_state_dict(torch.load('Batch_feature_norm_model'))
model = model.to(device)
opt = torch.optim.SGD(model.parameters(), lr=learning_rate, momentum=0.9)
fit(num_epochs, model, loss_fn, input_near, output_near)Epoch  0 near loss: 0.43399933 s1d1:  1.1771337 s1d2:  1.2721639 s1d3:  1.1272545 s2d1: 0.5581254 s2d4: 0.7407873 s2d5: 0.80058265 s3d1: 0.32291272 s3d6: 0.80094373 s3d7: 0.927961 s4d1: 0.9631928 s4d8: 1.1931956 s4d9: 0.5334821 test loss: 0.8574422870543786

</code>