eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I want to write the following complicated function using python scipy.integrate: where \phi and \Phi are the pdf and cdf of the standard normal respectively and the sum i != 1,2 is over the thetas in *theta, and the product s != 1,i,2 is over the thetas in *theta that is not theta_i To make things simpler, I break down the problem into a few easier pieces by defining a few more functions:  And here is what I have tried: import scipy.integrate as integrate from scipy.integrate import nquad from scipy.stats import norm import math import numpy as np def f(v, u, t1, ti, t2, *theta): prod = 1 for t in theta: prod = prod * (1 - norm.cdf(u + t2 - t)) return norm.pdf(v) * norm.cdf(v + ti - t1) * prod def g(v, u, t1, t2, *theta): S = 0 for ti in theta: S = S + integrate.quad(f, -np.inf, u + t2 - ti, args=(u, t1, ti, t2, *theta))[0] return S * norm.pdf(u) def P(t1, t2, *theta): return integrate.quad(g, -np.inf, np.inf, args=(t1, t2, *theta))[0]  But it doens't work because I think in the definition of P the integral is integrating w.r.t. the first variable, namely v but we should be integrating u but I don't know how we can do that. Is there any quick way to do it? For checking, here is what the correct P would produce: P(0.2, 0.1, 0.3, 0.4) = 0.08856347190679764 P(0.2, 0.1, 0.4, 0.3, 0.5) = 0.06094233268837703 

Answer: The first thing I did was move prod outside of f since it isn't a function of v. I realized that your prod includes the ti term, so I added a check for ts (renamed from t) equal to ti. You also have way too many variables for your functions. g is a function of u and the thetas and f is a function of v and the thetas, so they don't both need u and v. This isn't the most efficient code, but it works. from scipy.integrate import quad from scipy.stats import norm import numpy as np def f(v, t1, ti): return norm.pdf(v) * norm.cdf(v + ti - t1) def g(u, t1, t2, *theta): S = 0 for ti in theta: prod = 1 for ts in theta: if ts != ti: prod *= (1 - norm.cdf(u + t2 - ts)) S += prod*quad(f, -np.inf, u + t2 - ti, args=(t1, ti))[0] return S * norm.pdf(u) def P(t1, t2, *theta): return quad(g, -np.inf, np.inf, args=(t1, t2, *theta))[0] print(P(0.2, 0.1, 0.3, 0.4)) # 0.08856347187084088 print(P(0.2, 0.1, 0.4, 0.3, 0.5)) # 0.060942332693157915
Question: I have the following dataframe (except my actual data is over 25 years): import pandas as pd df = pd.DataFrame( dict( date=pd.date_range(start="2020-01-01", end="2020-12-31", freq="MS"), data=[1,2,3,4,5,6,7,8,9,10,11,12] ), ) df  Output:  date data 0 2020-01-01 1 1 2020-02-01 2 2 2020-03-01 3 3 2020-04-01 4 4 2020-05-01 5 5 2020-06-01 6 6 2020-07-01 7 7 2020-08-01 8 8 2020-09-01 9 9 2020-10-01 10 10 2020-11-01 11 11 2020-12-01 12  And I get different results with matplotlib and pandas default plotting: import matplotlib as mpl import matplotlib.dates as mdates import matplotlib.pyplot as plt fig = mpl.figure.Figure(constrained_layout=True) axs = fig.subplot_mosaic("ac;bd") ax = axs["a"] ax.bar(x="date", height="data", data=df, width=15) ax = axs["b"] ax.bar(x="date", height="data", data=df, width=15) locator = mdates.AutoDateLocator(minticks=12, maxticks=24) formatter = mdates.ConciseDateFormatter(locator) ax.xaxis.set_major_locator(locator) ax.xaxis.set_major_formatter(formatter) ax = axs["c"] df.plot.bar(x="date", y="data", ax=ax, legend=False) ax = axs["d"] df.plot.bar(x="date", y="data", ax=ax, legend=False, ) # incorrect year -> 1970 instead of 2020 locator = mdates.AutoDateLocator(minticks=12, maxticks=24) formatter = mdates.ConciseDateFormatter(locator) ax.xaxis.set_major_locator(locator) ax.xaxis.set_major_formatter(formatter) for k, ax in axs.items(): for label in ax.get_xticklabels(): label.set_rotation(40) label.set_horizontalalignment('right') fig  Output:  I would like to be able to use pandas for plotting but then format the ticks appropriately for a publication ready plot. However, it appears that I lose the date time information or get the incorrect year when using pandas. Is there a way to format the axis ticklabels using mdates features without using the data directly? i.e. if I resample the data, or slice in a different year, I'd like the axis to reflect that automatically.  Here's a more simple illustration of the issue I'm having: import matplotlib as mpl import matplotlib.dates as mdates import matplotlib.pyplot as plt fig = mpl.figure.Figure(constrained_layout=True) axs = fig.subplot_mosaic("a") ax = axs["a"] df.plot.bar(x="date", y="data", ax=ax, legend=False) # incorrect year -> 1970 instead of 2020 formatter = mdates.DateFormatter("%Y - %b") ax.xaxis.set_major_formatter(formatter) fig   The dates are all wrong when using DateFormatter.

Answer: When you are using a bar plot, the x-coordinates become 0, 1, 2, 3, etc. That's why mdates.DateFormatter returns 1970, as it treats these coordinates as seconds since epoch time. You can set the tick labels manually: ax.set_xticklabels(df["date"].dt.strftime("%Y - %b"))
Question: Update: This was fixed by pull/18251  I am new to Polars. I want to iterate over the groups created by grouping over the column where each cell of that column contains a list of two dates. I used the following (sample) piece of code to achieve and it used to work fine with polars==0.20.18 version: import polars as pl import datetime dt_str = [{'ts': datetime.date(2023, 7, 1), 'files': 'AGG_202307.xlsx', 'period_bins': [datetime.date(2023, 7, 1), datetime.date(2024, 1, 1)]}, {'ts': datetime.date(2023, 8, 1), 'files': 'AGG_202308.xlsx', 'period_bins': [datetime.date(2023, 7, 1), datetime.date(2024, 1, 1)]}, {'ts': datetime.date(2023, 11, 1), 'files': 'KFC_202311.xlsx', 'period_bins': [datetime.date(2023, 7, 1), datetime.date(2024, 1, 1)]}, {'ts': datetime.date(2024, 2, 1), 'files': 'KFC_202402.xlsx', 'period_bins': [datetime.date(2024, 1, 1), datetime.date(2024, 7, 1)]}] dt = pl.from_dicts(dt_str) df_groups = dt.group_by("period_bins") print(df_groups.all().to_dicts())  The above code does not work with polars==1.x and gives the following error: thread 'polars-0' panicked at crates/polars-row/src/encode.rs:289:15: not implemented: Date32 thread 'polars-1' panicked at crates/polars-row/src/encode.rs:289:15: not implemented: Date32 Traceback (most recent call last): File "testpad.py", line 18, in <module> print(df_groups.all().to_dicts()) File "python3.10/site-packages/polars/dataframe/group_by.py", line 430, in all return self.agg(F.all()) File "python3.10/site-packages/polars/dataframe/group_by.py", line 228, in agg self.df.lazy() File "python3.10/site-packages/polars/lazyframe/frame.py", line 2027, in collect return wrap_df(ldf.collect(callback)) pyo3_runtime.PanicException: not implemented: Date32  How do I fix this error?

Answer: You could group by the .hash() (or cast) as a workaround. (df.group_by(pl.col("period_bins").hash().alias("key")) .all() )  shape: (2, 4) ┌─────────────────────┬─────────────────────────────────┬─────────────────────────────────┬─────────────────────────────────┐ │ key ┆ ts ┆ files ┆ period_bins │ │ --- ┆ --- ┆ --- ┆ --- │ │ u64 ┆ list[date] ┆ list[str] ┆ list[list[date]] │ ╞═════════════════════╪═════════════════════════════════╪═════════════════════════════════╪═════════════════════════════════╡ │ 6836989170623494942 ┆ [2023-07-01, 2023-08-01, 2023-… ┆ ["AGG_202307.xlsx", "AGG_20230… ┆ [[2023-07-01, 2024-01-01], [20… │ │ 2692156858231355433 ┆ [2024-02-01] ┆ ["KFC_202402.xlsx"] ┆ [[2024-01-01, 2024-07-01]] │ └─────────────────────┴─────────────────────────────────┴─────────────────────────────────┴─────────────────────────────────┘
Question: I have several dataframes with an ID, a time of day, and a number. I would like to weight each dataframe number and then sum them for each id/time of day. As an example: weighted 0.2 ID TOD M 0 10 morning 1 1 13 afternoon 3 2 32 evening 2 3 10 evening 2 weighted 0.4 ID TOD W 0 10 morning 1 1 13 morning 3 2 32 afternoon 2 3 10 evening 3 weighted sum: ID TOD weighed_sum_mw 0 10 morning (0.2*1 + 0.4*1) 1 10 evening (0.2*2 + 0.4*3) 2 13 morning (0.4*3) 3 13 afternoon (0.4*2) 3 32 evening (0.2*2) 4 32 afternoon (0.4*2)  The following strategy works but is very memory consuming and I'm not sure if there's a way to do this without merging them. I also eventually only need the row with the time of day that has the largest sum for each ID so if that simplifies the process that works as well! (Tie breakers for equal max weighted sums will keep Afternoon, then Evening, then Morning first). I currently do this with 4 dataframes but may add more and they are approximately 10M rows each merged_oc= pd.merge(dfs[0], dfs[3], on=['ID', 'TIME_OF_DAY'], suffixes=('_O', '_C'), how='outer') merged_s = pd.merge(dfs[1], dfs[2], on=['ID', 'TIME_OF_DAY'], suffixes=('_W', 'M'), how='outer') # merge and weighted sum of O and C merged_oc['COUNTS_O_weighted_02']= merged_oc['COUNTS_O'].fillna(0).multiply(0.2) merged_oc['COUNTS_C_weighted_04'] = merged_oc['COUNTS_C'].fillna(0).multiply(0.4) merged_oc['COUNTS'] = merged_oc['COUNTS_O_weighted_02'] + merged_oc['COUNTS_C_weighted_04'] result_oc = merged_oc[['ID', 'TIME_OF_DAY', 'COUNTS', 'COUNTS_O_weighted_02', 'COUNTS_C_weighted_04']] merged_s['COUNTS_W_weighted_04'] = merged_s['COUNTS_W'].fillna(0).multiply(0.4) merged_s['COUNTS_M_weighted_04'] = merged_s['COUNTS_M'].fillna(0).multiply(0.4) merged_s['COUNTS'] = merged_s['COUNTS_W_weighted_04'] + merged_s['COUNTS_M_weighted_04'] result_s = merged_s[['ID', 'TIME_OF_DAY', 'COUNTS', 'COUNTS_W_weighted_04', 'COUNTS_M_weighted_04']] merged_final = pd.merge(result_oc, result_s, on=['ID', 'TIME_OF_DAY'], suffixes=('_OC', '_S'), how='outer') merged_final['COUNTS_OC']= merged_final['COUNTS_OC'].fillna(0) merged_final['COUNTS_S'] = merged_final['COUNTS_S'].fillna(0) merged_final['WEIGHTED_SUM'] = merged_final['COUNTS_OC'] + merged_final['COUNTS_SESSION'] merged_final = merged_final[['ID', 'TIME_OF_DAY', 'WEIGHTED_SUM', 'COUNTS_O_weighted_02', 'COUNTS_C_weighted_04', 'COUNTS_W_weighted_04', 'COUNTS_M_weighted_04']].fillna(0) 

Answer: IIUC, you can try pd.concat the dataframes after you set index and multiply by your weights for each dataframe, then use groupby and sum: df_out = pd.concat([df1_2.set_index(['ID', 'TOD']).mul(.2), df2_4.set_index(['ID', 'TOD']).mul(.4)])\ .sum(axis=1)\ .groupby(level=[0,1])\ .sum()\ .reset_index() df_out  Output:  ID TOD 0 0 10 evening 1.6 1 10 morning 0.6 2 13 afternoon 0.6 3 13 morning 1.2 4 32 afternoon 0.8 5 32 evening 0.4
Question: Some of my columns in my Polars Dataframe have the dtype pl.List(pl.Struct). I'm trying to replace these columns so that I get multiple columns that are lists of scalar values. Here's an example of a column I'm trying to change: import polars as pl df = pl.DataFrame({ "column_0": [ [{"field_1": "a", "field_2": 1}, {"field_1": "b", "field_2":2}], [{"field_1": "c", "field_2":3}] ] }) col_name = "column_0"  df.select( pl.col(col_name).list.eval( pl.element().struct.unnest() ) )  My expectation was that I'll get something like this: shape: (2, 2) ┌────────────┬───────────┐ │ field_1 ┆ field_2 │ │ --- ┆ --- │ │ list[str] ┆ list[i64] │ ╞════════════╪═══════════╡ │ ["a", "b"] ┆ [1, 2] │ │ ["c"] ┆ [3] │ └────────────┴───────────┘  Instead, I only get the last field (in this case, 'field_2'): shape: (2, 1) ┌───────────┐ │ column_0 │ │ --- │ │ list[i64] │ ╞═══════════╡ │ [1, 2] │ │ [3] │ └───────────┘ 

Answer: You could unpack the lists/structs with .explode() + .unnest() and group the rows back together. (df.with_row_index() .explode("column_0") .unnest("column_0") .group_by("index", maintain_order=True) .all() )  shape: (2, 3) ┌───────┬────────────┬───────────┐ │ index ┆ field_1 ┆ field_2 │ │ --- ┆ --- ┆ --- │ │ u32 ┆ list[str] ┆ list[i64] │ ╞═══════╪════════════╪═══════════╡ │ 0 ┆ ["a", "b"] ┆ [1, 2] │ │ 1 ┆ ["c"] ┆ [3] │ └───────┴────────────┴───────────┘
Question: Q: Is there a better way to do this, or the idea itself is wrong I have a processing class that creates something with multiple construction steps, such that the next function depends on the previous ones. I want to have dependencies specified like those in a makefile, and when the dependency does not exist, construct it. I currently use a decorator to achieve it but it feels non pythonic. Please take a look from typing import * from functools import wraps def step_method(dependency: Optional[dict[str, Callable]] = None): if dependency is None: dependency = {} def decorator(method): @wraps(method) def wrapper(self, *args, **kwargs): for attr, func in dependency.items(): if not getattr(self, attr): func(self) ret = method(self, *args, **kwargs) return ret return wrapper return decorator class StepClass: def __init__(self, base_val:int): self.base_val: int = base_val self.a = None self.b = [] self.c = None self.d = [] @step_method({}) def gen_a(self): self.a = self.base_val * 2 @step_method({'a': gen_a}) def create_b(self): self.b = [self.a] * 3 @step_method({ 'a': gen_a, 'b': create_b }) def gen_c(self): self.c = sum(self.b) * self.a @step_method({'c': gen_c}) def generate_d(self): self.d = list(range(self.c)) sc = StepClass(10) sc.base_val = 7 # allow changes before generating starts sc.b = [1, 2, 3] # allow dependency value injection sc.generate_d() print(sc.a, sc.b, sc.c, sc.d, sep='\n')  I also wonder if it's possible to detect the usage of variables automatically and generate them through a prespecified dict of function if they don't exist yet

Answer: This is a good use case of properties, with which you can generate values on demand so there's no need to build a dependency tree. As a convention the generated values are cached in attributes of names prefixed with an underscore. Since all your getter methods and setter methods are going to access attributes in a uniform way, you can programmatically create a getter and a setter method for each given calculation function when initializing a property subclass. class SettableCachedProperty(property): def __init__(self, func): def fget(instance): if (value := getattr(instance, name, None)) is None: setattr(self, name, value := func(instance)) return value def fset(instance, value): setattr(instance, name, value) name = '_' + func.__name__ super().__init__(fget, fset) class StepClass: def __init__(self, base_val): self.base_val = base_val @SettableCachedProperty def a(self): return self.base_val * 2 @SettableCachedProperty def b(self): return [self.a] * 3 @SettableCachedProperty def c(self): return sum(self.b) * self.a  so that (omitting d in your example for brevity): sc = StepClass(10) sc.base_val = 7 # allow changes before generating starts sc.b = [1, 2, 3] # allow dependency value injection print(sc.a, sc.b, sc.c, sep='\n')  outputs: 14 [1, 2, 3] 84  Demo here
Question: I was trying to implement rolling autocorrelation in polars, but got some weird results when there're nulls involved. The code is pretty simple. Let's say I have two dataframes df1 and df2: df1 = pl.DataFrame({'a': [1.06, 1.07, 0.93, 0.78, 0.85], 'lag_a': [1., 1.06, 1.07, 0.93, 0.78]}) df2 = pl.DataFrame({'a': [1., 1.06, 1.07, 0.93, 0.78, 0.85], 'lag_a': [None, 1., 1.06, 1.07, 0.93, 0.78]})  You can see that the only difference is that in df2, the first row for lag_a is None, because it's shifted from a. When I compute the rolling_corr for both dataframes, however, I got different results. # df1.select(pl.rolling_corr('a', 'lag_a', window_size=10, min_periods=5, ddof=1)) shape: (5, 1) ┌──────────┐ │ a │ │ --- │ │ f64 │ ╞══════════╡ │ null │ │ null │ │ null │ │ null │ │ 0.622047 │ └──────────┘  # df2.select(pl.rolling_corr('a', 'lag_a', window_size=10, min_periods=5, ddof=1)) shape: (6, 1) ┌───────────┐ │ a │ │ --- │ │ f64 │ ╞═══════════╡ │ null │ │ null │ │ null │ │ null │ │ null │ │ -0.219851 │ └───────────┘  The result from df1, i.e. 0.622047 is what I got from numpy.corrcoef as well. I wonder where the -0.219851 is coming from.

Answer: I think this is a bug in the Rust implementation of rolling_corr (in fairness, it is marked unstable in python). It looks it naively applies rolling_mean without first applying the joint null mask. So the rolling mean of a that's used in the computation is df2.get_column("a").rolling_mean(window_size=10, min_periods=5)  shape: (6,) Series: 'a' [f64] [ null null null null 0.968 0.948333 ]  That's the correct rolling mean in a vacuum, but in this case the first row of df2’s column a should be considered null because lag_a is null there, and so df2’s rolling mean should be the same as df1’s rolling mean, with an extra null up front. df1.get_column("a").rolling_mean(window_size=10, min_periods=5)  shape: (5,) Series: 'a' [f64] [ null null null null 0.938 ]  I'd suggest filing a bug report or even a PR. It doesn't look like a hard fix, it should just require precomputing the mask and applying filters to all expressions before calculating rolling stats on them. In the meantime, you can apply the mask yourself before computing the correlation: df2.with_columns( pl.when(pl.any_horizontal(pl.all().is_null())) .then(None) .otherwise(pl.all()) .name.keep() ).select(pl.rolling_corr("a", "lag_a", window_size=10, min_periods=5))  shape: (6, 1) ┌──────────┐ │ a │ │ --- │ │ f64 │ ╞══════════╡ │ null │ │ null │ │ null │ │ null │ │ null │ │ 0.622047 │ └──────────┘
Question: I've tried using a few different methods but all end up not working. I'm trying to read from a directory and iterate all the .desc files and inspect all the fields in each class. I'm trying to build a dependency tree with the outer classes being the parent nodes and the leaves being the nested classes. I'm trying to do this all dynamically based on a root folder containing the .desc files (or proto files). Nothing is statically typed. The error: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf8 in position 197: 'utf-8' codec can't decode byte 0xf8 in position 197: invalid start byte in field: google.protobuf.FileDescriptorProto.name def gen1(desc_file, db=None): if not db: db = symbol_database.Default() with open(desc_file, 'rb') as fh: fds = _descriptor_pool.DescriptorPool().AddSerializedFile(fh.read()) # <- this errors generated = {} for prot in fds.file: fd = db.pool.Add(prot) for name in fd.message_types_by_name: mdesc = fd.message_types_by_name[name] Klass = reflection.MakeClass(mdesc) generated[(fd.package, name)] = db.RegisterMessage(Klass) return generated 

Answer: The Protobuf hierarchy is confusing/complex. The following is a basic approach to enumerating the contents of a Protobuf descriptor file: from google.protobuf.descriptor_pb2 import FileDescriptorSet with open("descriptor.pb", 'rb') as descriptor: data = descriptor.read() fds = FileDescriptorSet.FromString(data) # file: FileDescriptorProto for file in fds.file: print(f"{file.package}/{file.name}") # service: ServiceDescriptorProto for service in file.service: print(service.name) # method: MethodDescriptorProto for method in service.method: print(method.name) # message: DescriptorProto for message_type in file.message_type: print(message_type.name) # field: FieldDescriptorProto for field in message_type.field: print(field.name)
Question: I'm trying to write a join operation between two Numpy arrays and was surprised to find Numpy's recfunctions.join_by doesn't handle duplicate values. The approach I'm taking is using the column to be joined and finding an index mapping between them. From looking online, majority of Numpy only solutions suffer the same problem of not being able to handle duplicates (you'll see what I mean down in the code section). I'm looking to stay entirely within the Numpy library, if at all possible to take advantage of vectorized operations, so ideally no native Python code, Pandas (for other reasons), or numpy-indexed. Below are a few questions I've looked at: A way to map one array onto another in numpy? Find index mapping between two numpy arrays Numpy: For every element in one array, find the index in another array Index mapping between two sorted partially overlapping numpy arrays For example, arrays X and Y which are to be joined using a column from each of them, x and y respectively. The mapping is defined as, and f is what I'm after mapping = f(x, y) x = y[mapping]  So for example, x = np.array([1,1,2,100]) y = np.array([1,2,3,4,5,6,7]) mapping = [0, 0, 1, -] # '-' indicates masked x = y[mapping]  From looking at similar questions onlinem, find the mapping from x to y there is np.where(np.isin(x,y)) which deduplicates values. There is also np.searchsorted(x,y) which doesn't handle duplicates in x at all. I'm wondering if there is something else that can be done. Below is not a correct mapping due to duplicates in x import numpy as np x = np.array([1,1,2,100]) y = np.array([1,2,3,4,5,6,7]) mapping = np.searchsorted(x, y) # [0 2 3 3 3 3 3]  This is also not a correct mapping because mapping needs to be the same length as x. import numpy as np x = np.array([1,1,2,100]) y = np.array([1,2,3,4,5,6,7]) mapping = np.where(np.isin(x, y))[0] # [0, 1, 2] 

Answer: Using np.isin() we can literally create a mask that shows us which values are already in the other array, when you have that you only need to figure out the indices. import numpy as np # Arrays to be joined x = np.array([1, 1, 2, 100, 4, 5, 3, 75]) y = np.array([1, 2, 3, 4, 5, 6, 7]) # Get mask with True and False values mask = np.isin(x, y) # [ True True True False True True True False] # Get indices of every element indices = np.searchsorted(y, x) # [0 0 1 7 3 4 2 7] # Match indices with mask mapping = np.where(mask, indices, -1) # [ 0 0 1 -1 3 4 2 -1]  We create the mask, get the indices, and then match the indices with the mask. Values that are not in y get the value -1 This solution fully stays inside the Numpy library
Question: Observe in the following code, creating an numpy array and calling the builtin python sum function produces different results than numpy.sum How is numpy's sum function implemented? And why is the result different? test = [.1]*10 test = [np.float64(x) for x in test] test[5]= np.float64(-.9) d = [np.asarray(test) for x in range(0,60000)] sum(sum(d))  outputs np.float64(-1.7473212210461497e-08)  but np.sum(d)  outputs np.float64(9.987344284922983e-12) 

Answer: Numpy uses pairwise summation:https://github.com/numpy/numpy/pull/3685 but python uses reduce summation. The answer is only partially related to FP inaccuracy because if I have an array of FP numbers and use the same algorithm to sum them, I should expect the same result if I sum them in the same order.
Question: My input x is a [1,256,60,120] shaped tensor. My Conv2d is defined as follows import torch.nn as nn conv2d = nn.Conv2d( 256, 256, kernel_size=2, stride=2, bias=False, ),  Some instances I see that conv2d(x).isinf().any() is True. Note that x.max() = tensor(5140., device='cuda:0', dtype=torch.float16). x.min() = tensor(0., device='cuda:0', dtype=torch.float16), conv2d.weight.max() =tensor(1.5796, device='cuda:0') conv2d.weight.min() = tensor(-0.8045, device='cuda:0') Why do I get infinity?

Answer: This is a case of numerical overflow. Consider: import torch import torch.nn as nn # set seed torch.manual_seed(42) # random values between 0 and 5140, like your values x = (torch.rand(1, 256, 60, 120) * 5140) # create conv layer conv2d = nn.Conv2d( 256, 256, kernel_size=2, stride=2, bias=False, ) # set conv weights to a similar range to your values torch.nn.init.uniform_(conv2d.weight, a=-0.8, b=1.57) # compute output y = conv2d(x) print(y.max()) > tensor(1334073.6250, grad_fn=<MaxBackward1>)  When I run the code above, I see a max output value of 1334073.6250. You are using fp16 values, which overflow at 32000. You're seeing inf values because your output values exceed the range of fp16. You should preprocess your inputs to have a more reasonable numeric range. Neural networks are very sensitive to numerical scale. Typically you want your input values to be roughly mean 0, variance 1. A max input value of 5140 is huge and likely to cause numeric issues in any situation.